Skip to content

Commit 572f1ef

Browse files
committed
util: use constructor name
When reaching the depth limit util.inspect always prints [Array] or [Object] no matter if it is a subclass or not. This fixes it by showing the actual constructor name instead.
1 parent 8f52ccc commit 572f1ef

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

lib/util.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -590,11 +590,9 @@ function formatValue(ctx, value, recurseTimes, ln) {
590590
return ctx.stylize('[Circular]', 'special');
591591

592592
if (recurseTimes != null) {
593-
if (recurseTimes < 0) {
594-
if (Array.isArray(value))
595-
return ctx.stylize('[Array]', 'special');
596-
return ctx.stylize('[Object]', 'special');
597-
}
593+
if (recurseTimes < 0)
594+
return ctx.stylize(`[${constructor ? constructor.name : 'Object'}]`,
595+
'special');
598596
recurseTimes -= 1;
599597
}
600598

test/parallel/test-util-inspect.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,10 @@ if (typeof Symbol !== 'undefined') {
994994
'MapSubclass { \'foo\' => 42 }');
995995
assert.strictEqual(util.inspect(new PromiseSubclass(() => {})),
996996
'PromiseSubclass { <pending> }');
997+
assert.strictEqual(
998+
util.inspect({ a: { b: new ArraySubclass([1, [2], 3]) } }, { depth: 1 }),
999+
'{ a: { b: [ArraySubclass] } }'
1000+
);
9971001
}
9981002

9991003
// Empty and circular before depth

0 commit comments

Comments
 (0)