code refactor

This commit is contained in:
MaysWind
2024-06-29 17:12:02 +08:00
parent c951285049
commit d9c8142c51
10 changed files with 64 additions and 60 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ import { mapStores } from 'pinia';
import { useSettingsStore } from '@/stores/setting.js';
import colorConstants from '@/consts/color.js';
import { formatPercent } from '@/lib/common.js';
import { formatPercent } from '@/lib/numeral.js';
export default {
props: [
+2 -1
View File
@@ -66,7 +66,8 @@
import { mapStores } from 'pinia';
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';
export default {
+1 -1
View File
@@ -81,7 +81,7 @@ import { mapStores } from 'pinia';
import { useSettingsStore } from '@/stores/setting.js';
import colorConstants from '@/consts/color.js';
import { formatPercent } from '@/lib/common.js';
import { formatPercent } from '@/lib/numeral.js';
export default {
props: [
-52
View File
@@ -109,58 +109,6 @@ export function getObjectOwnFieldCount(object) {
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) {
let length = 0;
+2 -1
View File
@@ -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) {
let str = num.toString();
+51
View File
@@ -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 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 {
getYearAndMonthFromUnixTime,
getYearMonthFirstUnixTime,
@@ -166,7 +166,8 @@ import { useRootStore } from '@/stores/index.js';
import { useSettingsStore } from '@/stores/setting.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 { startDownloadFile } from '@/lib/ui.js';
@@ -258,7 +258,8 @@ import { useStatisticsStore } from '@/stores/statistics.js';
import datetimeConstants from '@/consts/datetime.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 {
getShiftedDateRangeAndDateType,
getDateTypeByDateRange,
@@ -73,7 +73,7 @@ import { useRootStore } from '@/stores/index.js';
import { useSettingsStore } from '@/stores/setting.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';
export default {