Skip to content

Commit 0f58ae3

Browse files
committed
util: format() now formats bigint and booleans
This is necessary to distinguish them from other data types. PR-URL: #25046 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anto Aravinth <[email protected]>
1 parent 728b155 commit 0f58ae3

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

doc/api/util.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ util.format('%s:%s', 'foo');
245245
```
246246

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

251250
If there are more arguments passed to the `util.format()` method than the
252251
number of specifiers, the extra arguments are concatenated to the returned

lib/util.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,8 @@ function formatWithOptions(inspectOptions, ...args) {
174174

175175
while (a < args.length) {
176176
const value = args[a];
177-
// TODO(BridgeAR): This should apply for all besides strings. Especially
178-
// BigInt should be properly inspected.
179177
str += join;
180-
if (typeof value !== 'string' &&
181-
typeof value !== 'boolean' &&
182-
// eslint-disable-next-line valid-typeof
183-
typeof value !== 'bigint') {
184-
str += inspect(value, inspectOptions);
185-
} else {
186-
str += value;
187-
}
178+
str += typeof value !== 'string' ? inspect(value, inspectOptions) : value;
188179
join = ' ';
189180
a++;
190181
}

test/parallel/test-util-format.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,27 @@ assert.strictEqual(util.format(new BadCustomError('foo')),
316316
assert.strictEqual(util.format('1', '1'), '1 1');
317317
assert.strictEqual(util.format(1, '1'), '1 1');
318318
assert.strictEqual(util.format('1', 1), '1 1');
319-
assert.strictEqual(util.format(1, 1), '1 1');
319+
assert.strictEqual(util.format(1, -0), '1 -0');
320320
assert.strictEqual(util.format('1', () => {}), '1 [Function]');
321321
assert.strictEqual(util.format(1, () => {}), '1 [Function]');
322322
assert.strictEqual(util.format('1', "'"), "1 '");
323323
assert.strictEqual(util.format(1, "'"), "1 '");
324324
assert.strictEqual(util.format('1', 'number'), '1 number');
325325
assert.strictEqual(util.format(1, 'number'), '1 number');
326+
assert.strictEqual(util.format(5n), '5n');
327+
assert.strictEqual(util.format(5n, 5n), '5n 5n');
328+
329+
// Check `formatWithOptions`.
330+
assert.strictEqual(
331+
util.formatWithOptions(
332+
{ colors: true },
333+
true, undefined, Symbol(), 1, 5n, null, 'foobar'
334+
),
335+
'\u001b[33mtrue\u001b[39m ' +
336+
'\u001b[90mundefined\u001b[39m ' +
337+
'\u001b[32mSymbol()\u001b[39m ' +
338+
'\u001b[33m1\u001b[39m ' +
339+
'\u001b[33m5n\u001b[39m ' +
340+
'\u001b[1mnull\u001b[22m ' +
341+
'foobar'
342+
);

0 commit comments

Comments
 (0)