mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-19 01:04:25 +08:00
code refactor
This commit is contained in:
@@ -10,7 +10,7 @@ import { mapStores } from 'pinia';
|
|||||||
import { useSettingsStore } from '@/stores/setting.js';
|
import { useSettingsStore } from '@/stores/setting.js';
|
||||||
|
|
||||||
import colorConstants from '@/consts/color.js';
|
import colorConstants from '@/consts/color.js';
|
||||||
import { formatPercent } from '@/lib/common.js';
|
import { formatPercent } from '@/lib/numeral.js';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
props: [
|
props: [
|
||||||
|
|||||||
@@ -66,7 +66,8 @@
|
|||||||
import { mapStores } from 'pinia';
|
import { mapStores } from 'pinia';
|
||||||
import { useSettingsStore } from '@/stores/setting.js';
|
import { useSettingsStore } from '@/stores/setting.js';
|
||||||
|
|
||||||
import { isString, appendThousandsSeparator } from '@/lib/common.js';
|
import { isString } from '@/lib/common.js';
|
||||||
|
import { appendThousandsSeparator } from '@/lib/numeral.js';
|
||||||
import { numericCurrencyToString, stringCurrencyToNumeric } from '@/lib/currency.js';
|
import { numericCurrencyToString, stringCurrencyToNumeric } from '@/lib/currency.js';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ import { mapStores } from 'pinia';
|
|||||||
import { useSettingsStore } from '@/stores/setting.js';
|
import { useSettingsStore } from '@/stores/setting.js';
|
||||||
|
|
||||||
import colorConstants from '@/consts/color.js';
|
import colorConstants from '@/consts/color.js';
|
||||||
import { formatPercent } from '@/lib/common.js';
|
import { formatPercent } from '@/lib/numeral.js';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
props: [
|
props: [
|
||||||
|
|||||||
@@ -109,58 +109,6 @@ export function getObjectOwnFieldCount(object) {
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function appendThousandsSeparator(value, enable) {
|
|
||||||
if (!enable || value.length <= 3) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
const negative = value.charAt(0) === '-';
|
|
||||||
|
|
||||||
if (negative) {
|
|
||||||
value = value.substring(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
const dotPos = value.indexOf('.');
|
|
||||||
const integer = dotPos < 0 ? value : value.substring(0, dotPos);
|
|
||||||
const decimals = dotPos < 0 ? '' : value.substring(dotPos + 1, value.length);
|
|
||||||
|
|
||||||
const finalChars = [];
|
|
||||||
|
|
||||||
for (let i = 0; i < integer.length; i++) {
|
|
||||||
if (i % 3 === 0 && i > 0) {
|
|
||||||
finalChars.push(',');
|
|
||||||
}
|
|
||||||
|
|
||||||
finalChars.push(integer.charAt(integer.length - 1 - i));
|
|
||||||
}
|
|
||||||
|
|
||||||
finalChars.reverse();
|
|
||||||
|
|
||||||
let newInteger = finalChars.join('');
|
|
||||||
|
|
||||||
if (negative) {
|
|
||||||
newInteger = `-${newInteger}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dotPos < 0) {
|
|
||||||
return newInteger;
|
|
||||||
} else {
|
|
||||||
return `${newInteger}.${decimals}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function formatPercent(value, precision, lowPrecisionValue) {
|
|
||||||
const ratio = Math.pow(10, precision);
|
|
||||||
const normalizedValue = Math.floor(value * ratio);
|
|
||||||
|
|
||||||
if (value > 0 && normalizedValue < 1 && lowPrecisionValue) {
|
|
||||||
return lowPrecisionValue + '%';
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = normalizedValue / ratio;
|
|
||||||
return result + '%';
|
|
||||||
}
|
|
||||||
|
|
||||||
export function limitText(value, maxLength) {
|
export function limitText(value, maxLength) {
|
||||||
let length = 0;
|
let length = 0;
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -1,4 +1,5 @@
|
|||||||
import { isNumber, appendThousandsSeparator } from './common.js';
|
import { isNumber } from './common.js';
|
||||||
|
import { appendThousandsSeparator } from './numeral.js';
|
||||||
|
|
||||||
export function numericCurrencyToString(num, enableThousandsSeparator, trimTailZero) {
|
export function numericCurrencyToString(num, enableThousandsSeparator, trimTailZero) {
|
||||||
let str = num.toString();
|
let str = num.toString();
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
export function appendThousandsSeparator(value, enable) {
|
||||||
|
if (!enable || value.length <= 3) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
const negative = value.charAt(0) === '-';
|
||||||
|
|
||||||
|
if (negative) {
|
||||||
|
value = value.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const dotPos = value.indexOf('.');
|
||||||
|
const integer = dotPos < 0 ? value : value.substring(0, dotPos);
|
||||||
|
const decimals = dotPos < 0 ? '' : value.substring(dotPos + 1, value.length);
|
||||||
|
|
||||||
|
const finalChars = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < integer.length; i++) {
|
||||||
|
if (i % 3 === 0 && i > 0) {
|
||||||
|
finalChars.push(',');
|
||||||
|
}
|
||||||
|
|
||||||
|
finalChars.push(integer.charAt(integer.length - 1 - i));
|
||||||
|
}
|
||||||
|
|
||||||
|
finalChars.reverse();
|
||||||
|
|
||||||
|
let newInteger = finalChars.join('');
|
||||||
|
|
||||||
|
if (negative) {
|
||||||
|
newInteger = `-${newInteger}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dotPos < 0) {
|
||||||
|
return newInteger;
|
||||||
|
} else {
|
||||||
|
return `${newInteger}.${decimals}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatPercent(value, precision, lowPrecisionValue) {
|
||||||
|
const ratio = Math.pow(10, precision);
|
||||||
|
const normalizedValue = Math.floor(value * ratio);
|
||||||
|
|
||||||
|
if (value > 0 && normalizedValue < 1 && lowPrecisionValue) {
|
||||||
|
return lowPrecisionValue + '%';
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = normalizedValue / ratio;
|
||||||
|
return result + '%';
|
||||||
|
}
|
||||||
@@ -302,7 +302,8 @@ import { useStatisticsStore } from '@/stores/statistics.js';
|
|||||||
|
|
||||||
import datetimeConstants from '@/consts/datetime.js';
|
import datetimeConstants from '@/consts/datetime.js';
|
||||||
import statisticsConstants from '@/consts/statistics.js';
|
import statisticsConstants from '@/consts/statistics.js';
|
||||||
import { limitText, formatPercent } from '@/lib/common.js'
|
import { limitText } from '@/lib/common.js'
|
||||||
|
import { formatPercent } from '@/lib/numeral.js';
|
||||||
import {
|
import {
|
||||||
getYearAndMonthFromUnixTime,
|
getYearAndMonthFromUnixTime,
|
||||||
getYearMonthFirstUnixTime,
|
getYearMonthFirstUnixTime,
|
||||||
|
|||||||
@@ -166,7 +166,8 @@ import { useRootStore } from '@/stores/index.js';
|
|||||||
import { useSettingsStore } from '@/stores/setting.js';
|
import { useSettingsStore } from '@/stores/setting.js';
|
||||||
import { useUserStore } from '@/stores/user.js';
|
import { useUserStore } from '@/stores/user.js';
|
||||||
|
|
||||||
import {appendThousandsSeparator, isEquals} from '@/lib/common.js';
|
import { isEquals } from '@/lib/common.js';
|
||||||
|
import { appendThousandsSeparator } from '@/lib/numeral.js';
|
||||||
import { isDataExportingEnabled } from '@/lib/server_settings.js';
|
import { isDataExportingEnabled } from '@/lib/server_settings.js';
|
||||||
import { startDownloadFile } from '@/lib/ui.js';
|
import { startDownloadFile } from '@/lib/ui.js';
|
||||||
|
|
||||||
|
|||||||
@@ -258,7 +258,8 @@ import { useStatisticsStore } from '@/stores/statistics.js';
|
|||||||
|
|
||||||
import datetimeConstants from '@/consts/datetime.js';
|
import datetimeConstants from '@/consts/datetime.js';
|
||||||
import statisticsConstants from '@/consts/statistics.js';
|
import statisticsConstants from '@/consts/statistics.js';
|
||||||
import { getNameByKeyValue, limitText, formatPercent } from '@/lib/common.js'
|
import { getNameByKeyValue, limitText } from '@/lib/common.js'
|
||||||
|
import { formatPercent } from '@/lib/numeral.js'
|
||||||
import {
|
import {
|
||||||
getShiftedDateRangeAndDateType,
|
getShiftedDateRangeAndDateType,
|
||||||
getDateTypeByDateRange,
|
getDateTypeByDateRange,
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ import { useRootStore } from '@/stores/index.js';
|
|||||||
import { useSettingsStore } from '@/stores/setting.js';
|
import { useSettingsStore } from '@/stores/setting.js';
|
||||||
import { useUserStore } from '@/stores/user.js';
|
import { useUserStore } from '@/stores/user.js';
|
||||||
|
|
||||||
import { appendThousandsSeparator } from '@/lib/common.js';
|
import { appendThousandsSeparator } from '@/lib/numeral.js';
|
||||||
import { isDataExportingEnabled } from '@/lib/server_settings.js';
|
import { isDataExportingEnabled } from '@/lib/server_settings.js';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
|||||||
Reference in New Issue
Block a user