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 38706cc
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
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,9 @@ 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 38706cc

Please sign in to comment.