Skip to content

Commit

Permalink
util: refactor addNumericSeparator logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 16, 2021
1 parent 9d6168b commit 43d5713
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions lib/internal/util/inspect.js
Expand Up @@ -1458,15 +1458,11 @@ function handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) {
}

function addNumericSeparator(integerString) {
let result = '';
let i = integerString.length;
const start = integerString.startsWith('-') ? 1 : 0;
for (; i >= start + 4; i -= 3) {
result = `_${integerString.slice(i - 3, i)}${result}`;
}
return i === integerString.length ?
integerString :
`${integerString.slice(0, i)}${result}`;
return StringPrototypeReplace(
integerString,
/[0-9](?=(?:[0-9]{3})+(?![0-9]))/g,
'$&_'
);
}

function addNumericSeparatorEnd(integerString) {
Expand Down Expand Up @@ -1502,7 +1498,7 @@ function formatNumber(fn, number, numericSeparator) {
return fn(`${
addNumericSeparator(string)
}.${
addNumericSeparatorEnd(String(number).slice(string.length + 1))
StringPrototypeSlice(addNumericSeparatorEnd(String(number), string.length + 1))
}`, 'number');
}

Expand Down

0 comments on commit 43d5713

Please sign in to comment.