Skip to content

Commit 43d5713

Browse files
committed
util: refactor addNumericSeparator logic
1 parent 9d6168b commit 43d5713

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

lib/internal/util/inspect.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -1458,15 +1458,11 @@ function handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) {
14581458
}
14591459

14601460
function addNumericSeparator(integerString) {
1461-
let result = '';
1462-
let i = integerString.length;
1463-
const start = integerString.startsWith('-') ? 1 : 0;
1464-
for (; i >= start + 4; i -= 3) {
1465-
result = `_${integerString.slice(i - 3, i)}${result}`;
1466-
}
1467-
return i === integerString.length ?
1468-
integerString :
1469-
`${integerString.slice(0, i)}${result}`;
1461+
return StringPrototypeReplace(
1462+
integerString,
1463+
/[0-9](?=(?:[0-9]{3})+(?![0-9]))/g,
1464+
'$&_'
1465+
);
14701466
}
14711467

14721468
function addNumericSeparatorEnd(integerString) {
@@ -1502,7 +1498,7 @@ function formatNumber(fn, number, numericSeparator) {
15021498
return fn(`${
15031499
addNumericSeparator(string)
15041500
}.${
1505-
addNumericSeparatorEnd(String(number).slice(string.length + 1))
1501+
StringPrototypeSlice(addNumericSeparatorEnd(String(number), string.length + 1))
15061502
}`, 'number');
15071503
}
15081504

0 commit comments

Comments
 (0)