Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: better number formatters #27499

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ function formatPrimitive(fn, value, ctx) {
if (typeof value === 'undefined')
return fn('undefined', 'undefined');
// es6 symbol primitive
return fn(value.toString(), 'symbol');
return fn(SymbolPrototype.toString(value), 'symbol');
}

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

const emptyOptions = {};
function format(...args) {
return formatWithOptions(emptyOptions, ...args);
return formatWithOptions(undefined, ...args);
}


Expand Down Expand Up @@ -1509,16 +1508,14 @@ function formatWithOptions(inspectOptions, ...args) {
switch (nextChar) {
case 115: // 's'
const tempArg = args[++a];
if (typeof tempArg === 'object' && tempArg !== null) {
if (typeof tempArg !== 'string' &&
typeof tempArg !== 'function') {
tempStr = inspect(tempArg, {
...inspectOptions,
compact: 3,
colors: false,
depth: 0
});
// eslint-disable-next-line valid-typeof
} else if (typeof tempArg === 'bigint') {
tempStr = `${tempArg}n`;
} else {
tempStr = String(tempArg);
}
Expand All @@ -1534,7 +1531,7 @@ function formatWithOptions(inspectOptions, ...args) {
} else if (typeof tempNum === 'symbol') {
tempStr = 'NaN';
} else {
tempStr = `${Number(tempNum)}`;
tempStr = formatNumber(stylizeNoColor, Number(tempNum));
}
break;
case 79: // 'O'
Expand All @@ -1558,15 +1555,15 @@ function formatWithOptions(inspectOptions, ...args) {
} else if (typeof tempInteger === 'symbol') {
tempStr = 'NaN';
} else {
tempStr = `${parseInt(tempInteger)}`;
tempStr = formatNumber(stylizeNoColor, parseInt(tempInteger));
}
break;
case 102: // 'f'
const tempFloat = args[++a];
if (typeof tempFloat === 'symbol') {
tempStr = 'NaN';
} else {
tempStr = `${parseFloat(tempFloat)}`;
tempStr = formatNumber(stylizeNoColor, parseFloat(tempFloat));
}
break;
case 37: // '%'
Expand Down
7 changes: 6 additions & 1 deletion test/parallel/test-util-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ assert.strictEqual(util.format('%d', '42'), '42');
assert.strictEqual(util.format('%d', '42.0'), '42');
assert.strictEqual(util.format('%d', 1.5), '1.5');
assert.strictEqual(util.format('%d', -0.5), '-0.5');
assert.strictEqual(util.format('%d', -0.0), '-0');
assert.strictEqual(util.format('%d', ''), '0');
assert.strictEqual(util.format('%d', ' -0.000'), '-0');
assert.strictEqual(util.format('%d', Symbol()), 'NaN');
assert.strictEqual(util.format('%d %d', 42, 43), '42 43');
assert.strictEqual(util.format('%d %d', 42), '42 %d');
Expand All @@ -77,7 +79,7 @@ assert.strictEqual(util.format('%i', 42), '42');
assert.strictEqual(util.format('%i', '42'), '42');
assert.strictEqual(util.format('%i', '42.0'), '42');
assert.strictEqual(util.format('%i', 1.5), '1');
assert.strictEqual(util.format('%i', -0.5), '0');
assert.strictEqual(util.format('%i', -0.5), '-0');
assert.strictEqual(util.format('%i', ''), 'NaN');
assert.strictEqual(util.format('%i', Symbol()), 'NaN');
assert.strictEqual(util.format('%i %i', 42, 43), '42 43');
Expand Down Expand Up @@ -110,6 +112,7 @@ assert.strictEqual(util.format('%f'), '%f');
assert.strictEqual(util.format('%f', 42.0), '42');
assert.strictEqual(util.format('%f', 42), '42');
assert.strictEqual(util.format('%f', '42'), '42');
assert.strictEqual(util.format('%f', '-0.0'), '-0');
assert.strictEqual(util.format('%f', '42.0'), '42');
assert.strictEqual(util.format('%f', 1.5), '1.5');
assert.strictEqual(util.format('%f', -0.5), '-0.5');
Expand All @@ -127,6 +130,8 @@ assert.strictEqual(util.format('%s', null), 'null');
assert.strictEqual(util.format('%s', 'foo'), 'foo');
assert.strictEqual(util.format('%s', 42), '42');
assert.strictEqual(util.format('%s', '42'), '42');
assert.strictEqual(util.format('%s', -0), '-0');
assert.strictEqual(util.format('%s', '-0.0'), '-0.0');
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(util.format('%s %s', 42, 43), '42 43');
assert.strictEqual(util.format('%s %s', 42), '42 %s');
assert.strictEqual(util.format('%s', 42n), '42n');
Expand Down