Skip to content

Commit 8dae89b

Browse files
BridgeARTrott
authored andcommitted
util: better number formatters
This makes sure the `%d`, `%f`, `%i` and `%s` formatters properly visualize `-0`. On top, this also switches to using a safer symbol toString function by using the primordial function. PR-URL: #27499 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent c903c99 commit 8dae89b

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

lib/internal/util/inspect.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ function formatPrimitive(fn, value, ctx) {
10781078
if (typeof value === 'undefined')
10791079
return fn('undefined', 'undefined');
10801080
// es6 symbol primitive
1081-
return fn(value.toString(), 'symbol');
1081+
return fn(SymbolPrototype.toString(value), 'symbol');
10821082
}
10831083

10841084
function formatNamespaceObject(ctx, value, recurseTimes, keys) {
@@ -1484,9 +1484,8 @@ function reduceToSingleString(
14841484
return `${braces[0]}${ln}${join(output, `,\n${indentation} `)} ${braces[1]}`;
14851485
}
14861486

1487-
const emptyOptions = {};
14881487
function format(...args) {
1489-
return formatWithOptions(emptyOptions, ...args);
1488+
return formatWithOptions(undefined, ...args);
14901489
}
14911490

14921491

@@ -1532,16 +1531,14 @@ function formatWithOptions(inspectOptions, ...args) {
15321531
switch (nextChar) {
15331532
case 115: // 's'
15341533
const tempArg = args[++a];
1535-
if (typeof tempArg === 'object' && tempArg !== null) {
1534+
if (typeof tempArg !== 'string' &&
1535+
typeof tempArg !== 'function') {
15361536
tempStr = inspect(tempArg, {
15371537
...inspectOptions,
15381538
compact: 3,
15391539
colors: false,
15401540
depth: 0
15411541
});
1542-
// eslint-disable-next-line valid-typeof
1543-
} else if (typeof tempArg === 'bigint') {
1544-
tempStr = `${tempArg}n`;
15451542
} else {
15461543
tempStr = String(tempArg);
15471544
}
@@ -1557,7 +1554,7 @@ function formatWithOptions(inspectOptions, ...args) {
15571554
} else if (typeof tempNum === 'symbol') {
15581555
tempStr = 'NaN';
15591556
} else {
1560-
tempStr = `${Number(tempNum)}`;
1557+
tempStr = formatNumber(stylizeNoColor, Number(tempNum));
15611558
}
15621559
break;
15631560
case 79: // 'O'
@@ -1581,15 +1578,15 @@ function formatWithOptions(inspectOptions, ...args) {
15811578
} else if (typeof tempInteger === 'symbol') {
15821579
tempStr = 'NaN';
15831580
} else {
1584-
tempStr = `${parseInt(tempInteger)}`;
1581+
tempStr = formatNumber(stylizeNoColor, parseInt(tempInteger));
15851582
}
15861583
break;
15871584
case 102: // 'f'
15881585
const tempFloat = args[++a];
15891586
if (typeof tempFloat === 'symbol') {
15901587
tempStr = 'NaN';
15911588
} else {
1592-
tempStr = `${parseFloat(tempFloat)}`;
1589+
tempStr = formatNumber(stylizeNoColor, parseFloat(tempFloat));
15931590
}
15941591
break;
15951592
case 37: // '%'

test/parallel/test-util-format.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ assert.strictEqual(util.format('%d', '42'), '42');
5353
assert.strictEqual(util.format('%d', '42.0'), '42');
5454
assert.strictEqual(util.format('%d', 1.5), '1.5');
5555
assert.strictEqual(util.format('%d', -0.5), '-0.5');
56+
assert.strictEqual(util.format('%d', -0.0), '-0');
5657
assert.strictEqual(util.format('%d', ''), '0');
58+
assert.strictEqual(util.format('%d', ' -0.000'), '-0');
5759
assert.strictEqual(util.format('%d', Symbol()), 'NaN');
5860
assert.strictEqual(util.format('%d %d', 42, 43), '42 43');
5961
assert.strictEqual(util.format('%d %d', 42), '42 %d');
@@ -77,7 +79,7 @@ assert.strictEqual(util.format('%i', 42), '42');
7779
assert.strictEqual(util.format('%i', '42'), '42');
7880
assert.strictEqual(util.format('%i', '42.0'), '42');
7981
assert.strictEqual(util.format('%i', 1.5), '1');
80-
assert.strictEqual(util.format('%i', -0.5), '0');
82+
assert.strictEqual(util.format('%i', -0.5), '-0');
8183
assert.strictEqual(util.format('%i', ''), 'NaN');
8284
assert.strictEqual(util.format('%i', Symbol()), 'NaN');
8385
assert.strictEqual(util.format('%i %i', 42, 43), '42 43');
@@ -110,6 +112,7 @@ assert.strictEqual(util.format('%f'), '%f');
110112
assert.strictEqual(util.format('%f', 42.0), '42');
111113
assert.strictEqual(util.format('%f', 42), '42');
112114
assert.strictEqual(util.format('%f', '42'), '42');
115+
assert.strictEqual(util.format('%f', '-0.0'), '-0');
113116
assert.strictEqual(util.format('%f', '42.0'), '42');
114117
assert.strictEqual(util.format('%f', 1.5), '1.5');
115118
assert.strictEqual(util.format('%f', -0.5), '-0.5');
@@ -127,6 +130,8 @@ assert.strictEqual(util.format('%s', null), 'null');
127130
assert.strictEqual(util.format('%s', 'foo'), 'foo');
128131
assert.strictEqual(util.format('%s', 42), '42');
129132
assert.strictEqual(util.format('%s', '42'), '42');
133+
assert.strictEqual(util.format('%s', -0), '-0');
134+
assert.strictEqual(util.format('%s', '-0.0'), '-0.0');
130135
assert.strictEqual(util.format('%s %s', 42, 43), '42 43');
131136
assert.strictEqual(util.format('%s %s', 42), '42 %s');
132137
assert.strictEqual(util.format('%s', 42n), '42n');

0 commit comments

Comments
 (0)