From 4690d1fe245397b88166e532f776e481b2d9d3c8 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 12 Dec 2018 17:14:41 +0100 Subject: [PATCH] util: `format()` now formats bigint and booleans This is necessary to distinguish them from other data types. --- doc/api/util.md | 3 +-- lib/util.js | 11 +---------- test/parallel/test-util-format.js | 19 ++++++++++++++++++- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/doc/api/util.md b/doc/api/util.md index 0b9e47739487e9..cf3cd698fe61ec 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -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 diff --git a/lib/util.js b/lib/util.js index ef28427cd87bc8..f48d93d124738a 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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++; } diff --git a/test/parallel/test-util-format.js b/test/parallel/test-util-format.js index 066b4de58c1399..a90e52946b07ef 100644 --- a/test/parallel/test-util-format.js +++ b/test/parallel/test-util-format.js @@ -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' +);