Skip to content

Commit 747a893

Browse files
refactor: trimTrailingZeros
1 parent bc4eba1 commit 747a893

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/numbers/custom-number-format.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const EMPTY = "";
1515

1616
const literalRegExp = /(\\.)|(['][^']*[']?)|(["][^"]*["]?)/g;
1717
const trailingZerosRegExp = /(\.(?:[0-9]*[1-9])?)0+$/g;
18+
const trailingPointRegExp = /\.$/;
1819
const commaRegExp = /\,/g;
1920

2021
function setFormatLiterals(formatOptions) {
@@ -33,18 +34,15 @@ function setFormatLiterals(formatOptions) {
3334
}
3435

3536
function trimTrailingZeros(value, lastZero) {
36-
let result;
37+
let trimRegex;
38+
3739
if (lastZero === 0) {
38-
result = value.replace(trailingZerosRegExp, '$1');
40+
trimRegex = trailingZerosRegExp;
3941
} else {
40-
result = value.replace(new RegExp(`(\\.[0-9]{${ lastZero }}[1-9]*)0+$`, 'g'), '$1');
41-
}
42-
43-
if (result.charAt(result.length - 1) === POINT) {
44-
result = result.substr(0, result.length - 1);
42+
trimRegex = new RegExp(`(\\.[0-9]{${ lastZero }}[1-9]*)0+$`, 'g');
4543
}
4644

47-
return result;
45+
return value.replace(trimRegex, '$1').replace(trailingPointRegExp, '');
4846
}
4947

5048
function roundNumber(formatOptions) {

0 commit comments

Comments
 (0)