Skip to content

Commit 4e8db7f

Browse files
watildeaduh95
authored andcommitted
util: fix -0 formatting when numericSeparator is enabled
Signed-off-by: Daijiro Wachi <daijiro.wachi@gmail.com> PR-URL: #63815 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Jordan Harband <ljharb@gmail.com>
1 parent f29949e commit 4e8db7f

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

lib/internal/util/inspect.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,11 +1917,12 @@ function addNumericSeparatorEnd(integerString) {
19171917
const remainingText = (remaining) => `... ${remaining} more item${remaining > 1 ? 's' : ''}`;
19181918

19191919
function formatNumber(fn, number, numericSeparator) {
1920+
// Format -0 as '-0'. Checking `number === -0` won't distinguish 0 from -0.
1921+
// String(-0) === '0', so this must be checked before any String() conversion.
1922+
if (ObjectIs(number, -0)) {
1923+
return fn('-0', 'number');
1924+
}
19201925
if (!numericSeparator) {
1921-
// Format -0 as '-0'. Checking `number === -0` won't distinguish 0 from -0.
1922-
if (ObjectIs(number, -0)) {
1923-
return fn('-0', 'number');
1924-
}
19251926
return fn(`${number}`, 'number');
19261927
}
19271928

test/parallel/test-util-inspect.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3793,6 +3793,7 @@ assert.strictEqual(
37933793
assert.strictEqual(util.inspect(NaN), 'NaN');
37943794
assert.strictEqual(util.inspect(Infinity), 'Infinity');
37953795
assert.strictEqual(util.inspect(-Infinity), '-Infinity');
3796+
assert.strictEqual(util.inspect(-0), '-0');
37963797

37973798
assert.strictEqual(
37983799
util.inspect(new Float64Array([100_000_000])),
@@ -3824,6 +3825,9 @@ assert.strictEqual(
38243825
'-123_456_789.123_456_78'
38253826
);
38263827

3828+
// -0 should be formatted as '-0' even with numericSeparator enabled
3829+
assert.strictEqual(util.inspect(-0, { numericSeparator: true }), '-0');
3830+
38273831
// Regression test for https://github.com/nodejs/node/issues/59376
38283832
// numericSeparator should work correctly for negative fractional numbers
38293833
{

0 commit comments

Comments
 (0)