diff options
Diffstat (limited to 'lib/internal/util/inspect.js')
-rw-r--r-- | lib/internal/util/inspect.js | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 07757f3fe0..e46a18633c 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -93,6 +93,10 @@ const { NativeModule } = require('internal/bootstrap/loaders'); let hexSlice; +const builtInObjects = new Set( + Object.getOwnPropertyNames(global).filter((e) => /^([A-Z][a-z]+)+$/.test(e)) +); + const inspectDefaultOptions = Object.seal({ showHidden: false, depth: 2, @@ -1541,16 +1545,33 @@ function formatWithOptions(inspectOptions, ...args) { switch (nextChar) { case 115: // 's' const tempArg = args[++a]; - if (typeof tempArg !== 'string' && - typeof tempArg !== 'function') { - tempStr = inspect(tempArg, { - ...inspectOptions, - compact: 3, - colors: false, - depth: 0 - }); + if (typeof tempArg === 'number') { + tempStr = formatNumber(stylizeNoColor, tempArg); + // eslint-disable-next-line valid-typeof + } else if (typeof tempArg === 'bigint') { + tempStr = `${tempArg}n`; } else { - tempStr = String(tempArg); + let constr; + if (typeof tempArg !== 'object' || + tempArg === null || + typeof tempArg.toString === 'function' && + // A direct own property. + (hasOwnProperty(tempArg, 'toString') || + // A direct own property on the constructor prototype in + // case the constructor is not an built-in object. + (constr = tempArg.constructor) && + !builtInObjects.has(constr.name) && + constr.prototype && + hasOwnProperty(constr.prototype, 'toString'))) { + tempStr = String(tempArg); + } else { + tempStr = inspect(tempArg, { + ...inspectOptions, + compact: 3, + colors: false, + depth: 0 + }); + } } break; case 106: // 'j' |