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
-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 + '%';
}