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: format() now formats bigint and booleans #25046

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
3 changes: 1 addition & 2 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ util.format('%s:%s', 'foo');
```

Values that are not part of the format string are formatted using
`util.inspect()` if their type is either `'object'`, `'symbol'`, `'function'`
or `'number'` and using `String()` in all other cases.
`util.inspect()` if their type is not `string`.

If there are more arguments passed to the `util.format()` method than the
number of specifiers, the extra arguments are concatenated to the returned
Expand Down
11 changes: 1 addition & 10 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,8 @@ function formatWithOptions(inspectOptions, ...args) {

while (a < args.length) {
const value = args[a];
// TODO(BridgeAR): This should apply for all besides strings. Especially
// BigInt should be properly inspected.
str += join;
if (typeof value !== 'string' &&
typeof value !== 'boolean' &&
// eslint-disable-next-line valid-typeof
typeof value !== 'bigint') {
str += inspect(value, inspectOptions);
} else {
str += value;
}
str += typeof value !== 'string' ? inspect(value, inspectOptions) : value;
join = ' ';
a++;
}
Expand Down
19 changes: 18 additions & 1 deletion test/parallel/test-util-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,27 @@ assert.strictEqual(util.format(new BadCustomError('foo')),
assert.strictEqual(util.format('1', '1'), '1 1');
assert.strictEqual(util.format(1, '1'), '1 1');
assert.strictEqual(util.format('1', 1), '1 1');
assert.strictEqual(util.format(1, 1), '1 1');
assert.strictEqual(util.format(1, -0), '1 -0');
assert.strictEqual(util.format('1', () => {}), '1 [Function]');
assert.strictEqual(util.format(1, () => {}), '1 [Function]');
assert.strictEqual(util.format('1', "'"), "1 '");
assert.strictEqual(util.format(1, "'"), "1 '");
assert.strictEqual(util.format('1', 'number'), '1 number');
assert.strictEqual(util.format(1, 'number'), '1 number');
assert.strictEqual(util.format(5n), '5n');
assert.strictEqual(util.format(5n, 5n), '5n 5n');

// Check `formatWithOptions`.
assert.strictEqual(
util.formatWithOptions(
{ colors: true },
true, undefined, Symbol(), 1, 5n, null, 'foobar'
),
'\u001b[33mtrue\u001b[39m ' +
'\u001b[90mundefined\u001b[39m ' +
'\u001b[32mSymbol()\u001b[39m ' +
'\u001b[33m1\u001b[39m ' +
'\u001b[33m5n\u001b[39m ' +
'\u001b[1mnull\u001b[22m ' +
'foobar'
);