Skip to content

Commit ab59989

Browse files
committed
util: add Set and map size to inspect output
This adds the size of a set and map to the output. This aligns the output with the one from Chromium. PR-URL: #30225 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent eeae598 commit ab59989

File tree

4 files changed

+54
-63
lines changed

4 files changed

+54
-63
lines changed

doc/api/util.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ console.log(util.inspect(o, { compact: true, depth: 5, breakLength: 80 }));
576576
// 'test',
577577
// 'foo' ] ],
578578
// 4 ],
579-
// b: Map { 'za' => 1, 'zb' => 'test' } }
579+
// b: Map(2) { 'za' => 1, 'zb' => 'test' } }
580580

581581
// Setting `compact` to false changes the output to be more reader friendly.
582582
console.log(util.inspect(o, { compact: false, depth: 5, breakLength: 80 }));
@@ -597,7 +597,7 @@ console.log(util.inspect(o, { compact: false, depth: 5, breakLength: 80 }));
597597
// ],
598598
// 4
599599
// ],
600-
// b: Map {
600+
// b: Map(2) {
601601
// 'za' => 1,
602602
// 'zb' => 'test'
603603
// }
@@ -639,9 +639,9 @@ const o1 = {
639639
c: new Set([2, 3, 1])
640640
};
641641
console.log(inspect(o1, { sorted: true }));
642-
// { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set { 1, 2, 3 } }
642+
// { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set(3) { 1, 2, 3 } }
643643
console.log(inspect(o1, { sorted: (a, b) => b.localeCompare(a) }));
644-
// { c: Set { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }
644+
// { c: Set(3) { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }
645645

646646
const o2 = {
647647
c: new Set([2, 1, 3]),

lib/internal/util/inspect.js

+4-12
Original file line numberDiff line numberDiff line change
@@ -778,10 +778,10 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
778778
if (constructor !== null) {
779779
if (constructor === tag)
780780
tag = '';
781-
prefix = getPrefix(`${constructor}`, tag, '');
781+
prefix = getPrefix(`${constructor}(${size})`, tag, '');
782782
formatter = formatSet.bind(null, value, size);
783783
} else {
784-
prefix = getPrefix(constructor, tag, 'Set');
784+
prefix = getPrefix(constructor, tag, `Set(${size})`);
785785
formatter = formatSet.bind(null, SetPrototypeValues(value), size);
786786
}
787787
if (size === 0 && keys.length === 0 && protoProps === undefined)
@@ -794,10 +794,10 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
794794
if (constructor !== null) {
795795
if (constructor === tag)
796796
tag = '';
797-
prefix = getPrefix(`${constructor}`, tag, '');
797+
prefix = getPrefix(`${constructor}(${size})`, tag, '');
798798
formatter = formatMap.bind(null, value, size);
799799
} else {
800-
prefix = getPrefix(constructor, tag, 'Map');
800+
prefix = getPrefix(constructor, tag, `Map(${size})`);
801801
formatter = formatMap.bind(null, MapPrototypeEntries(value), size);
802802
}
803803
if (size === 0 && keys.length === 0 && protoProps === undefined)
@@ -1437,11 +1437,6 @@ function formatSet(value, size, ctx, ignored, recurseTimes) {
14371437
output.push(formatValue(ctx, v, recurseTimes));
14381438
}
14391439
ctx.indentationLvl -= 2;
1440-
// With `showHidden`, `length` will display as a hidden property for
1441-
// arrays. For consistency's sake, do the same for `size`, even though this
1442-
// property isn't selected by ObjectGetOwnPropertyNames().
1443-
if (ctx.showHidden)
1444-
output.push(`[size]: ${ctx.stylize(`${size}`, 'number')}`);
14451440
return output;
14461441
}
14471442

@@ -1453,9 +1448,6 @@ function formatMap(value, size, ctx, ignored, recurseTimes) {
14531448
formatValue(ctx, v, recurseTimes));
14541449
}
14551450
ctx.indentationLvl -= 2;
1456-
// See comment in formatSet
1457-
if (ctx.showHidden)
1458-
output.push(`[size]: ${ctx.stylize(`${size}`, 'number')}`);
14591451
return output;
14601452
}
14611453

test/parallel/test-assert-deep.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ assertNotDeepOrStrict(
524524
{
525525
code: 'ERR_ASSERTION',
526526
message: `${defaultMsgStartFull}\n\n` +
527-
" Map {\n+ 1 => 1\n- 1 => '1'\n }"
527+
" Map(1) {\n+ 1 => 1\n- 1 => '1'\n }"
528528
}
529529
);
530530
}

test/parallel/test-util-inspect.js

+45-46
Original file line numberDiff line numberDiff line change
@@ -577,9 +577,9 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
577577
let obj = vm.runInNewContext('(function(){return {}})()', {});
578578
assert.strictEqual(util.inspect(obj), '{}');
579579
obj = vm.runInNewContext('var m=new Map();m.set(1,2);m', {});
580-
assert.strictEqual(util.inspect(obj), 'Map { 1 => 2 }');
580+
assert.strictEqual(util.inspect(obj), 'Map(1) { 1 => 2 }');
581581
obj = vm.runInNewContext('var s=new Set();s.add(1);s.add(2);s', {});
582-
assert.strictEqual(util.inspect(obj), 'Set { 1, 2 }');
582+
assert.strictEqual(util.inspect(obj), 'Set(2) { 1, 2 }');
583583
obj = vm.runInNewContext('fn=function(){};new Promise(fn,fn)', {});
584584
assert.strictEqual(util.inspect(obj), 'Promise { <pending> }');
585585
}
@@ -1068,47 +1068,53 @@ if (typeof Symbol !== 'undefined') {
10681068

10691069
// Test Set.
10701070
{
1071-
assert.strictEqual(util.inspect(new Set()), 'Set {}');
1072-
assert.strictEqual(util.inspect(new Set([1, 2, 3])), 'Set { 1, 2, 3 }');
1071+
assert.strictEqual(util.inspect(new Set()), 'Set(0) {}');
1072+
assert.strictEqual(util.inspect(new Set([1, 2, 3])), 'Set(3) { 1, 2, 3 }');
10731073
const set = new Set(['foo']);
10741074
set.bar = 42;
10751075
assert.strictEqual(
10761076
util.inspect(set, { showHidden: true }),
1077-
"Set { 'foo', [size]: 1, bar: 42 }"
1077+
"Set(1) { 'foo', bar: 42 }"
10781078
);
10791079
}
10801080

10811081
// Test circular Set.
10821082
{
10831083
const set = new Set();
10841084
set.add(set);
1085-
assert.strictEqual(util.inspect(set), '<ref *1> Set { [Circular *1] }');
1085+
assert.strictEqual(util.inspect(set), '<ref *1> Set(1) { [Circular *1] }');
10861086
}
10871087

10881088
// Test Map.
10891089
{
1090-
assert.strictEqual(util.inspect(new Map()), 'Map {}');
1090+
assert.strictEqual(util.inspect(new Map()), 'Map(0) {}');
10911091
assert.strictEqual(util.inspect(new Map([[1, 'a'], [2, 'b'], [3, 'c']])),
1092-
"Map { 1 => 'a', 2 => 'b', 3 => 'c' }");
1092+
"Map(3) { 1 => 'a', 2 => 'b', 3 => 'c' }");
10931093
const map = new Map([['foo', null]]);
10941094
map.bar = 42;
10951095
assert.strictEqual(util.inspect(map, true),
1096-
"Map { 'foo' => null, [size]: 1, bar: 42 }");
1096+
"Map(1) { 'foo' => null, bar: 42 }");
10971097
}
10981098

10991099
// Test circular Map.
11001100
{
11011101
const map = new Map();
11021102
map.set(map, 'map');
1103-
assert.strictEqual(inspect(map), "<ref *1> Map { [Circular *1] => 'map' }");
1103+
assert.strictEqual(
1104+
inspect(map),
1105+
"<ref *1> Map(1) { [Circular *1] => 'map' }"
1106+
);
11041107
map.set(map, map);
11051108
assert.strictEqual(
11061109
inspect(map),
1107-
'<ref *1> Map { [Circular *1] => [Circular *1] }'
1110+
'<ref *1> Map(1) { [Circular *1] => [Circular *1] }'
11081111
);
11091112
map.delete(map);
11101113
map.set('map', map);
1111-
assert.strictEqual(inspect(map), "<ref *1> Map { 'map' => [Circular *1] }");
1114+
assert.strictEqual(
1115+
inspect(map),
1116+
"<ref *1> Map(1) { 'map' => [Circular *1] }"
1117+
);
11121118
}
11131119

11141120
// Test multiple circular references.
@@ -1274,10 +1280,10 @@ if (typeof Symbol !== 'undefined') {
12741280
});
12751281

12761282
checkAlignment(obj, '{', " 'X': null", '}');
1277-
checkAlignment(new Set(bigArray), 'Set {', ' X', '}');
1283+
checkAlignment(new Set(bigArray), 'Set(100) {', ' X', '}');
12781284
checkAlignment(
12791285
new Map(bigArray.map((number) => [number, null])),
1280-
'Map {', ' X => null', '}'
1286+
'Map(100) {', ' X => null', '}'
12811287
);
12821288
}
12831289

@@ -1297,9 +1303,9 @@ if (typeof Symbol !== 'undefined') {
12971303
assert.strictEqual(util.inspect(new ArraySubclass(1, 2, 3)),
12981304
'ArraySubclass [ 1, 2, 3 ]');
12991305
assert.strictEqual(util.inspect(new SetSubclass([1, 2, 3])),
1300-
'SetSubclass [Set] { 1, 2, 3 }');
1306+
'SetSubclass(3) [Set] { 1, 2, 3 }');
13011307
assert.strictEqual(util.inspect(new MapSubclass([['foo', 42]])),
1302-
"MapSubclass [Map] { 'foo' => 42 }");
1308+
"MapSubclass(1) [Map] { 'foo' => 42 }");
13031309
assert.strictEqual(util.inspect(new PromiseSubclass(() => {})),
13041310
'PromiseSubclass [Promise] { <pending> }');
13051311
assert.strictEqual(
@@ -1558,7 +1564,7 @@ util.inspect(process);
15581564
" 'test',",
15591565
" 'foo' ] ],",
15601566
' 4 ],',
1561-
" b: Map { 'za' => 1, 'zb' => 'test' } }",
1567+
" b: Map(2) { 'za' => 1, 'zb' => 'test' } }",
15621568
].join('\n');
15631569
assert.strictEqual(out, expect);
15641570

@@ -1579,7 +1585,7 @@ util.inspect(process);
15791585
' ],',
15801586
' 4',
15811587
' ],',
1582-
' b: Map {',
1588+
' b: Map(2) {',
15831589
" 'za' => 1,",
15841590
" 'zb' => 'test'",
15851591
' }',
@@ -1659,18 +1665,17 @@ util.inspect(process);
16591665

16601666
let out = util.inspect(map, { compact: false, showHidden: true, depth: 9 });
16611667
let expected = [
1662-
'Map {',
1668+
'Map(2) {',
16631669
' Promise {',
16641670
' [',
16651671
' [',
16661672
' 1,',
1667-
' Set {',
1673+
' Set(1) {',
16681674
' [',
16691675
' 1,',
16701676
' 2,',
16711677
' [length]: 2',
1672-
' ],',
1673-
' [size]: 1',
1678+
' ]',
16741679
' },',
16751680
' [length]: 2',
16761681
' ],',
@@ -1704,8 +1709,7 @@ util.inspect(process);
17041709
' }',
17051710
' ],',
17061711
' [Circular *1]',
1707-
' },',
1708-
' [size]: 2',
1712+
' }',
17091713
'}'
17101714
].join('\n');
17111715

@@ -1714,12 +1718,12 @@ util.inspect(process);
17141718
out = util.inspect(map, { compact: 2, showHidden: true, depth: 9 });
17151719

17161720
expected = [
1717-
'Map {',
1721+
'Map(2) {',
17181722
' Promise {',
17191723
' [',
17201724
' [',
17211725
' 1,',
1722-
' Set { [ 1, 2, [length]: 2 ], [size]: 1 },',
1726+
' Set(1) { [ 1, 2, [length]: 2 ] },',
17231727
' [length]: 2',
17241728
' ],',
17251729
' [length]: 1',
@@ -1740,8 +1744,7 @@ util.inspect(process);
17401744
' [buffer]: ArrayBuffer { byteLength: 0, foo: true }',
17411745
' ],',
17421746
' [Circular *1]',
1743-
' },',
1744-
' [size]: 2',
1747+
' }',
17451748
'}'
17461749
].join('\n');
17471750

@@ -1751,14 +1754,13 @@ util.inspect(process);
17511754
showHidden: true, depth: 9, breakLength: 4, compact: true
17521755
});
17531756
expected = [
1754-
'Map {',
1757+
'Map(2) {',
17551758
' Promise {',
17561759
' [ [ 1,',
1757-
' Set {',
1760+
' Set(1) {',
17581761
' [ 1,',
17591762
' 2,',
1760-
' [length]: 2 ],',
1761-
' [size]: 1 },',
1763+
' [length]: 2 ] },',
17621764
' [length]: 2 ],',
17631765
' [length]: 1 ] } => Uint8Array [',
17641766
' [BYTES_PER_ELEMENT]: 1,',
@@ -1780,8 +1782,7 @@ util.inspect(process);
17801782
' [buffer]: ArrayBuffer {',
17811783
' byteLength: 0,',
17821784
' foo: true } ],',
1783-
' [Circular *1] },',
1784-
' [size]: 2 }'
1785+
' [Circular *1] } }'
17851786
].join('\n');
17861787

17871788
assert.strict.equal(out, expected);
@@ -1945,8 +1946,8 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
19451946
[[1, 2], '[ 1, 2 ]'],
19461947
[[, , 5, , , , ], '[ <2 empty items>, 5, <3 empty items> ]'],
19471948
[{ a: 5 }, '{ a: 5 }'],
1948-
[new Set([1, 2]), 'Set { 1, 2 }'],
1949-
[new Map([[1, 2]]), 'Map { 1 => 2 }'],
1949+
[new Set([1, 2]), 'Set(2) { 1, 2 }'],
1950+
[new Map([[1, 2]]), 'Map(1) { 1 => 2 }'],
19501951
[new Set([1, 2]).entries(), '[Set Entries] { [ 1, 1 ], [ 2, 2 ] }'],
19511952
[new Map([[1, 2]]).keys(), '[Map Iterator] { 1 }'],
19521953
[new Date(2000), '1970-01-01T00:00:02.000Z'],
@@ -1977,8 +1978,8 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
19771978
// Verify that having no prototype still produces nice results.
19781979
[
19791980
[[1, 3, 4], '[Array: null prototype] [ 1, 3, 4 ]'],
1980-
[new Set([1, 2]), '[Set: null prototype] { 1, 2 }'],
1981-
[new Map([[1, 2]]), '[Map: null prototype] { 1 => 2 }'],
1981+
[new Set([1, 2]), '[Set(2): null prototype] { 1, 2 }'],
1982+
[new Map([[1, 2]]), '[Map(1): null prototype] { 1 => 2 }'],
19821983
[new Promise((resolve) => setTimeout(resolve, 10)),
19831984
'[Promise: null prototype] { <pending> }'],
19841985
[new WeakSet(), '[WeakSet: null prototype] { <items unknown> }'],
@@ -2219,7 +2220,7 @@ assert.strictEqual(
22192220
value: iterator,
22202221
configurable: true
22212222
});
2222-
assert.strictEqual(util.inspect(obj), '[Set: null prototype] { 1, 2 }');
2223+
assert.strictEqual(util.inspect(obj), '[Set(2): null prototype] { 1, 2 }');
22232224
Object.defineProperty(obj, Symbol.iterator, {
22242225
value: true,
22252226
configurable: true
@@ -2231,7 +2232,7 @@ assert.strictEqual(
22312232
});
22322233
assert.strictEqual(
22332234
util.inspect(obj),
2234-
'[Set: null prototype] { 1, 2, size: NaN }'
2235+
'[Set(2): null prototype] { 1, 2, size: NaN }'
22352236
);
22362237
}
22372238

@@ -2264,7 +2265,7 @@ assert.strictEqual(
22642265
getset.foo = new Set([[{ a: true }, 2, {}], 'foobar', { x: 1 }]);
22652266
assert.strictEqual(
22662267
inspect(getset, { getters: true }),
2267-
'{\n foo: [Getter/Setter] Set { [ [Object], 2, {} ], ' +
2268+
'{\n foo: [Getter/Setter] Set(3) { [ [Object], 2, {} ], ' +
22682269
"'foobar', { x: 1 } },\n inc: [Getter: NaN]\n}");
22692270
}
22702271

@@ -2655,12 +2656,11 @@ assert.strictEqual(
26552656

26562657
assert.strictEqual(
26572658
inspect(bar),
2658-
'Bar [Map] { prop: true, prop2: true, abc: true }'
2659+
'Bar(0) [Map] { prop: true, prop2: true, abc: true }'
26592660
);
26602661
assert.strictEqual(
26612662
inspect(bar, { showHidden: true, getters: true, colors: false }),
2662-
'Bar [Map] {\n' +
2663-
' [size]: 0,\n' +
2663+
'Bar(0) [Map] {\n' +
26642664
' prop: true,\n' +
26652665
' prop2: true,\n' +
26662666
' abc: true,\n' +
@@ -2670,8 +2670,7 @@ assert.strictEqual(
26702670
);
26712671
assert.strictEqual(
26722672
inspect(bar, { showHidden: true, getters: false, colors: true }),
2673-
'Bar [Map] {\n' +
2674-
' [size]: \x1B[33m0\x1B[39m,\n' +
2673+
'Bar(0) [Map] {\n' +
26752674
' prop: \x1B[33mtrue\x1B[39m,\n' +
26762675
' prop2: \x1B[33mtrue\x1B[39m,\n' +
26772676
' abc: \x1B[33mtrue\x1B[39m,\n' +

0 commit comments

Comments
 (0)