8000 util: better number formatters · nodejs/node@caab7d4 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit caab7d4

Browse files
BridgeARtargos
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 <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 99e4a57 commit caab7d4

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

lib/internal/util/inspect.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ function formatPrimitive(fn, value, ctx) {
10551055
if (typeof value === 'undefined')
10561056
return fn('undefined', 'undefined');
10571057
// es6 symbol primitive
1058-
return fn(value.toString(), 'symbol');
1058+
return fn(SymbolPrototype.toString(value), 'symbol');
10591059
}
10601060

10611061
function formatNamespaceObject(ctx, value, recurseTimes, keys) {
@@ -1461,9 +1461,8 @@ function reduceToSingleString(
14611461
return `${braces[0]}${ln}${join(output, `,\n${indentation} `)} ${braces[1]}`;
14621462
}
14631463

1464-
const emptyOptions = {};
14651464
function format(...args) {
1466-
return formatWithOptions(emptyOptions, ...args);
1465+
return formatWithOptions(undefined, ...args);
14671466
}
14681467

14691468

@@ -1509,16 +1508,14 @@ function formatWithOptions(inspectOptions, ...args) {
15091508
switch (nextChar) {
15101509
case 115: // 's'
15111510
const tempArg = args[++a];
1512-
if (typeof tempArg === 'object' && tempArg !== null) {
1511+
if (typeof tempArg !== 'string' &&
1512+
typeof tempArg !== 'function') {
15131513
tempStr = inspect(tempArg, {
15141514
...inspectOptions,
15151515
compact: 3,
15161516
colors: false,
15171517
depth: 0
15181518
});
1519-
// eslint-disable-next-line valid-typeof
1520-
} else if (typeof tempArg === 'bigint') {
1521-
tempStr = `${tempArg}n`;
15221519
} else {
15231520
tempStr = String(tempArg);
15241521
}
@@ -1534,7 +1531,7 @@ function formatWithOptions(inspectOptions, ...args) {
15341531
} else if (typeof tempNum === 'symbol') {
15351532
tempStr = 'NaN';
15361533
} else {
1537-
tempStr = `${Number(tempNum)}`;
1534+
tempStr = formatNumber(stylizeNoColor, Number(tempNum));
15381535
}
15391536
break;
15401537
case 79: // 'O'
@@ -1558,15 +1555,15 @@ function formatWithOptions(inspectOptions, ...args) {
15581555
} else if (typeof tempInteger === 'symbol') {
15591556
tempStr = 'NaN';
15601557
} else {
1561-
tempStr = `${parseInt(tempInteger)}`;
1558+
tempStr = formatNumber(stylizeNoColor, parseInt(tempInteger));
15621559
}
15631560
break;
15641561
case 102: // 'f'
15651562
const tempFloat = args[++a];
15661563
if (typeof tempFloat === 'symbol') {
15671564
tempStr = 'NaN';
15681565
} else {
1569-
tempStr = `${parseFloat(tempFloat)}`;
1566+
tempStr = formatNumber(stylizeNoColor, parseFloat(tempFloat));
15701567
}
15711568
break;
15721569
case 37: // '%'

test/parallel/test-util-format.js

+6-1
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)
0