Skip to content

Commit f62a767

Browse files
BridgeARMylesBorins
authored andcommitted
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 <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent f830a7d commit f62a767

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
@@ -773,10 +773,10 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
773773
if (constructor !== null) {
774774
if (constructor === tag)
775775
tag = '';
776-
prefix = getPrefix(`${constructor}`, tag, '');
776+
prefix = getPrefix(`${constructor}(${size})`, tag, '');
777777
formatter = formatSet.bind(null, value, size);
778778
} else {
779-
prefix = getPrefix(constructor, tag, 'Set');
779+
prefix = getPrefix(constructor, tag, `Set(${size})`);
780780
formatter = formatSet.bind(null, SetPrototypeValues(value), size);
781781
}
782782
if (size === 0 && keys.length === 0 && protoProps === undefined)
@@ -789,10 +789,10 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
789789
if (constructor !== null) {
790790
if (constructor === tag)
791791
tag = '';
792-
prefix = getPrefix(`${constructor}`, tag, '');
792+
prefix = getPrefix(`${constructor}(${size})`, tag, '');
793793
formatter = formatMap.bind(null, value, size);
794794
} else {
795-
prefix = getPrefix(constructor, tag, 'Map');
795+
prefix = getPrefix(constructor, tag, `Map(${size})`);
796796
formatter = formatMap.bind(null, MapPrototypeEntries(value), size);
797797
}
798798
if (size === 0 && keys.length === 0 && protoProps === undefined)
@@ -1432,11 +1432,6 @@ function formatSet(value, size, ctx, ignored, recurseTimes) {
14321432
output.push(formatValue(ctx, v, recurseTimes));
14331433
}
14341434
ctx.indentationLvl -= 2;
1435-
// With `showHidden`, `length` will display as a hidden property for
1436-
// arrays. For consistency's sake, do the same for `size`, even though this
1437-
// property isn't selected by ObjectGetOwnPropertyNames().
1438-
if (ctx.showHidden)
1439-
output.push(`[size]: ${ctx.stylize(`${size}`, 'number')}`);
14401435
return output;
14411436
}
14421437

@@ -1448,9 +1443,6 @@ function formatMap(value, size, ctx, ignored, recurseTimes) {
14481443
formatValue(ctx, v, recurseTimes));
14491444
}
14501445
ctx.indentationLvl -= 2;
1451-
// See comment in formatSet
1452-
if (ctx.showHidden)
1453-
output.push(`[size]: ${ctx.stylize(`${size}`, 'number')}`);
14541446
return output;
14551447
}
14561448

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
@@ -576,9 +576,9 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
576576
let obj = vm.runInNewContext('(function(){return {}})()', {});
577577
assert.strictEqual(util.inspect(obj), '{}');
578578
obj = vm.runInNewContext('var m=new Map();m.set(1,2);m', {});
579-
assert.strictEqual(util.inspect(obj), 'Map { 1 => 2 }');
579+
assert.strictEqual(util.inspect(obj), 'Map(1) { 1 => 2 }');
580580
obj = vm.runInNewContext('var s=new Set();s.add(1);s.add(2);s', {});
581-
assert.strictEqual(util.inspect(obj), 'Set { 1, 2 }');
581+
assert.strictEqual(util.inspect(obj), 'Set(2) { 1, 2 }');
582582
obj = vm.runInNewContext('fn=function(){};new Promise(fn,fn)', {});
583583
assert.strictEqual(util.inspect(obj), 'Promise { <pending> }');
584584
}
@@ -1067,47 +1067,53 @@ if (typeof Symbol !== 'undefined') {
10671067

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

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

10871087
// Test Map.
10881088
{
1089-
assert.strictEqual(util.inspect(new Map()), 'Map {}');
1089+
assert.strictEqual(util.inspect(new Map()), 'Map(0) {}');
10901090
assert.strictEqual(util.inspect(new Map([[1, 'a'], [2, 'b'], [3, 'c']])),
1091-
"Map { 1 => 'a', 2 => 'b', 3 => 'c' }");
1091+
"Map(3) { 1 => 'a', 2 => 'b', 3 => 'c' }");
10921092
const map = new Map([['foo', null]]);
10931093
map.bar = 42;
10941094
assert.strictEqual(util.inspect(map, true),
1095-
"Map { 'foo' => null, [size]: 1, bar: 42 }");
1095+
"Map(1) { 'foo' => null, bar: 42 }");
10961096
}
10971097

10981098
// Test circular Map.
10991099
{
11001100
const map = new Map();
11011101
map.set(map, 'map');
1102-
assert.strictEqual(inspect(map), "<ref *1> Map { [Circular *1] => 'map' }");
1102+
assert.strictEqual(
1103+
inspect(map),
1104+
"<ref *1> Map(1) { [Circular *1] => 'map' }"
1105+
);
11031106
map.set(map, map);
11041107
assert.strictEqual(
11051108
inspect(map),
1106-
'<ref *1> Map { [Circular *1] => [Circular *1] }'
1109+
'<ref *1> Map(1) { [Circular *1] => [Circular *1] }'
11071110
);
11081111
map.delete(map);
11091112
map.set('map', map);
1110-
assert.strictEqual(inspect(map), "<ref *1> Map { 'map' => [Circular *1] }");
1113+
assert.strictEqual(
1114+
inspect(map),
1115+
"<ref *1> Map(1) { 'map' => [Circular *1] }"
1116+
);
11111117
}
11121118

11131119
// Test multiple circular references.
@@ -1273,10 +1279,10 @@ if (typeof Symbol !== 'undefined') {
12731279
});
12741280

12751281
checkAlignment(obj, '{', " 'X': null", '}');
1276-
checkAlignment(new Set(bigArray), 'Set {', ' X', '}');
1282+
checkAlignment(new Set(bigArray), 'Set(100) {', ' X', '}');
12771283
checkAlignment(
12781284
new Map(bigArray.map((number) => [number, null])),
1279-
'Map {', ' X => null', '}'
1285+
'Map(100) {', ' X => null', '}'
12801286
);
12811287
}
12821288

@@ -1296,9 +1302,9 @@ if (typeof Symbol !== 'undefined') {
12961302
assert.strictEqual(util.inspect(new ArraySubclass(1, 2, 3)),
12971303
'ArraySubclass [ 1, 2, 3 ]');
12981304
assert.strictEqual(util.inspect(new SetSubclass([1, 2, 3])),
1299-
'SetSubclass [Set] { 1, 2, 3 }');
1305+
'SetSubclass(3) [Set] { 1, 2, 3 }');
13001306
assert.strictEqual(util.inspect(new MapSubclass([['foo', 42]])),
1301-
"MapSubclass [Map] { 'foo' => 42 }");
1307+
"MapSubclass(1) [Map] { 'foo' => 42 }");
13021308
assert.strictEqual(util.inspect(new PromiseSubclass(() => {})),
13031309
'PromiseSubclass [Promise] { <pending> }');
13041310
assert.strictEqual(
@@ -1557,7 +1563,7 @@ util.inspect(process);
15571563
" 'test',",
15581564
" 'foo' ] ],",
15591565
' 4 ],',
1560-
" b: Map { 'za' => 1, 'zb' => 'test' } }",
1566+
" b: Map(2) { 'za' => 1, 'zb' => 'test' } }",
15611567
].join('\n');
15621568
assert.strictEqual(out, expect);
15631569

@@ -1578,7 +1584,7 @@ util.inspect(process);
15781584
' ],',
15791585
' 4',
15801586
' ],',
1581-
' b: Map {',
1587+
' b: Map(2) {',
15821588
" 'za' => 1,",
15831589
" 'zb' => 'test'",
15841590
' }',
@@ -1658,18 +1664,17 @@ util.inspect(process);
16581664

16591665
let out = util.inspect(map, { compact: false, showHidden: true, depth: 9 });
16601666
let expected = [
1661-
'Map {',
1667+
'Map(2) {',
16621668
' Promise {',
16631669
' [',
16641670
' [',
16651671
' 1,',
1666-
' Set {',
1672+
' Set(1) {',
16671673
' [',
16681674
' 1,',
16691675
' 2,',
16701676
' [length]: 2',
1671-
' ],',
1672-
' [size]: 1',
1677+
' ]',
16731678
' },',
16741679
' [length]: 2',
16751680
' ],',
@@ -1703,8 +1708,7 @@ util.inspect(process);
17031708
' }',
17041709
' ],',
17051710
' [Circular *1]',
1706-
' },',
1707-
' [size]: 2',
1711+
' }',
17081712
'}'
17091713
].join('\n');
17101714

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

17151719
expected = [
1716-
'Map {',
1720+
'Map(2) {',
17171721
' Promise {',
17181722
' [',
17191723
' [',
17201724
' 1,',
1721-
' Set { [ 1, 2, [length]: 2 ], [size]: 1 },',
1725+
' Set(1) { [ 1, 2, [length]: 2 ] },',
17221726
' [length]: 2',
17231727
' ],',
17241728
' [length]: 1',
@@ -1739,8 +1743,7 @@ util.inspect(process);
17391743
' [buffer]: ArrayBuffer { byteLength: 0, foo: true }',
17401744
' ],',
17411745
' [Circular *1]',
1742-
' },',
1743-
' [size]: 2',
1746+
' }',
17441747
'}'
17451748
].join('\n');
17461749

@@ -1750,14 +1753,13 @@ util.inspect(process);
17501753
showHidden: true, depth: 9, breakLength: 4, compact: true
17511754
});
17521755
expected = [
1753-
'Map {',
1756+
'Map(2) {',
17541757
' Promise {',
17551758
' [ [ 1,',
1756-
' Set {',
1759+
' Set(1) {',
17571760
' [ 1,',
17581761
' 2,',
1759-
' [length]: 2 ],',
1760-
' [size]: 1 },',
1762+
' [length]: 2 ] },',
17611763
' [length]: 2 ],',
17621764
' [length]: 1 ] } => Uint8Array [',
17631765
' [BYTES_PER_ELEMENT]: 1,',
@@ -1779,8 +1781,7 @@ util.inspect(process);
17791781
' [buffer]: ArrayBuffer {',
17801782
' byteLength: 0,',
17811783
' foo: true } ],',
1782-
' [Circular *1] },',
1783-
' [size]: 2 }'
1784+
' [Circular *1] } }'
17841785
].join('\n');
17851786

17861787
assert.strict.equal(out, expected);
@@ -1944,8 +1945,8 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
19441945
[[1, 2], '[ 1, 2 ]'],
19451946
[[, , 5, , , , ], '[ <2 empty items>, 5, <3 empty items> ]'],
19461947
[{ a: 5 }, '{ a: 5 }'],
1947-
[new Set([1, 2]), 'Set { 1, 2 }'],
1948-
[new Map([[1, 2]]), 'Map { 1 => 2 }'],
1948+
[new Set([1, 2]), 'Set(2) { 1, 2 }'],
1949+
[new Map([[1, 2]]), 'Map(1) { 1 => 2 }'],
19491950
[new Set([1, 2]).entries(), '[Set Entries] { [ 1, 1 ], [ 2, 2 ] }'],
19501951
[new Map([[1, 2]]).keys(), '[Map Iterator] { 1 }'],
19511952
[new Date(2000), '1970-01-01T00:00:02.000Z'],
@@ -1976,8 +1977,8 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
19761977
// Verify that having no prototype still produces nice results.
19771978
[
19781979
[[1, 3, 4], '[Array: null prototype] [ 1, 3, 4 ]'],
1979-
[new Set([1, 2]), '[Set: null prototype] { 1, 2 }'],
1980-
[new Map([[1, 2]]), '[Map: null prototype] { 1 => 2 }'],
1980+
[new Set([1, 2]), '[Set(2): null prototype] { 1, 2 }'],
1981+
[new Map([[1, 2]]), '[Map(1): null prototype] { 1 => 2 }'],
19811982
[new Promise((resolve) => setTimeout(resolve, 10)),
19821983
'[Promise: null prototype] { <pending> }'],
19831984
[new WeakSet(), '[WeakSet: null prototype] { <items unknown> }'],
@@ -2218,7 +2219,7 @@ assert.strictEqual(
22182219
value: iterator,
22192220
configurable: true
22202221
});
2221-
assert.strictEqual(util.inspect(obj), '[Set: null prototype] { 1, 2 }');
2222+
assert.strictEqual(util.inspect(obj), '[Set(2): null prototype] { 1, 2 }');
22222223
Object.defineProperty(obj, Symbol.iterator, {
22232224
value: true,
22242225
configurable: true
@@ -2230,7 +2231,7 @@ assert.strictEqual(
22302231
});
22312232
assert.strictEqual(
22322233
util.inspect(obj),
2233-
'[Set: null prototype] { 1, 2, size: NaN }'
2234+
'[Set(2): null prototype] { 1, 2, size: NaN }'
22342235
);
22352236
}
22362237

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

@@ -2654,12 +2655,11 @@ assert.strictEqual(
26542655

26552656
assert.strictEqual(
26562657
inspect(bar),
2657-
'Bar [Map] { prop: true, prop2: true, abc: true }'
2658+
'Bar(0) [Map] { prop: true, prop2: true, abc: true }'
26582659
);
26592660
assert.strictEqual(
26602661
inspect(bar, { showHidden: true, getters: true, colors: false }),
2661-
'Bar [Map] {\n' +
2662-
' [size]: 0,\n' +
2662+
'Bar(0) [Map] {\n' +
26632663
' prop: true,\n' +
26642664
' prop2: true,\n' +
26652665
' abc: true,\n' +
@@ -2669,8 +2669,7 @@ assert.strictEqual(
26692669
);
26702670
assert.strictEqual(
26712671
inspect(bar, { showHidden: true, getters: false, colors: true }),
2672-
'Bar [Map] {\n' +
2673-
' [size]: \x1B[33m0\x1B[39m,\n' +
2672+
'Bar(0) [Map] {\n' +
26742673
' prop: \x1B[33mtrue\x1B[39m,\n' +
26752674
' prop2: \x1B[33mtrue\x1B[39m,\n' +
26762675
' abc: \x1B[33mtrue\x1B[39m,\n' +

0 commit comments

Comments
 (0)