From a9bf6652b5353f2098d4c0cd0eb77d17e02e164d Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 26 Mar 2019 15:53:11 +0100 Subject: util: use minimal object inspection with %s specifier MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This improves `util.format()` by returning more meaningful results when using `%s` as specifier and any object as value. Besides that `BigInt` will also be represented with an `n` at the end to indicate that it's of type `BigInt`. PR-URL: https://github.com/nodejs/node/pull/26927 Reviewed-By: Michaƫl Zasso Reviewed-By: Yongsheng Zhang --- lib/internal/util/inspect.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'lib/internal/util/inspect.js') diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 52924a4f70..f25c2eaca7 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1446,7 +1446,20 @@ function formatWithOptions(inspectOptions, ...args) { if (a + 1 !== args.length) { switch (nextChar) { case 115: // 's' - tempStr = String(args[++a]); + const tempArg = args[++a]; + if (typeof tempArg === 'object' && tempArg !== null) { + tempStr = inspect(tempArg, { + ...inspectOptions, + compact: 3, + colors: false, + depth: 0 + }); + // eslint-disable-next-line valid-typeof + } else if (typeof tempArg === 'bigint') { + tempStr = `${tempArg}n`; + } else { + tempStr = String(tempArg); + } break; case 106: // 'j' tempStr = tryStringify(args[++a]); -- cgit v1.2.3