util: format()
now formats bigint and booleans
This is necessary to distinguish them from other data types. PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
728b155870
commit
0f58ae392b
@ -245,8 +245,7 @@ util.format('%s:%s', 'foo');
|
|||||||
```
|
```
|
||||||
|
|
||||||
Values that are not part of the format string are formatted using
|
Values that are not part of the format string are formatted using
|
||||||
`util.inspect()` if their type is either `'object'`, `'symbol'`, `'function'`
|
`util.inspect()` if their type is not `string`.
|
||||||
or `'number'` and using `String()` in all other cases.
|
|
||||||
|
|
||||||
If there are more arguments passed to the `util.format()` method than the
|
If there are more arguments passed to the `util.format()` method than the
|
||||||
number of specifiers, the extra arguments are concatenated to the returned
|
number of specifiers, the extra arguments are concatenated to the returned
|
||||||
|
11
lib/util.js
11
lib/util.js
@ -174,17 +174,8 @@ function formatWithOptions(inspectOptions, ...args) {
|
|||||||
|
|
||||||
while (a < args.length) {
|
while (a < args.length) {
|
||||||
const value = args[a];
|
const value = args[a];
|
||||||
// TODO(BridgeAR): This should apply for all besides strings. Especially
|
|
||||||
// BigInt should be properly inspected.
|
|
||||||
str += join;
|
str += join;
|
||||||
if (typeof value !== 'string' &&
|
str += typeof value !== 'string' ? inspect(value, inspectOptions) : value;
|
||||||
typeof value !== 'boolean' &&
|
|
||||||
// eslint-disable-next-line valid-typeof
|
|
||||||
typeof value !== 'bigint') {
|
|
||||||
str += inspect(value, inspectOptions);
|
|
||||||
} else {
|
|
||||||
str += value;
|
|
||||||
}
|
|
||||||
join = ' ';
|
join = ' ';
|
||||||
a++;
|
a++;
|
||||||
}
|
}
|
||||||
|
@ -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', 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 [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, "'"), "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(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'
|
||||||
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user