Skip to content

Commit

Permalink
util: format() now formats bigint and booleans
Browse files Browse the repository at this point in the history
This is necessary to distinguish them from other data types.

PR-URL: nodejs#25046
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
  • Loading branch information
BridgeAR committed Dec 19, 2018
1 parent 728b155 commit 0f58ae3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
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'
);

0 comments on commit 0f58ae3

Please sign in to comment.