Skip to content

Commit cf2c603

Browse files
cola119guangwong
authored andcommitted
util: add maxArrayLength option to Set and Map
PR-URL: nodejs/node#43576 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 275fb48 commit cf2c603

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

doc/api/util.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,11 @@ stream.write('With ES6');
483483
<!-- YAML
484484
added: v0.3.0
485485
changes:
486-
- version: v16.14.0
486+
- version: REPLACEME
487+
pr-url: https://github.com/nodejs/node/pull/43576
488+
description: add support for `maxArrayLength` when inspecting `Set` and `Map`.
489+
- version:
490+
- v16.14.0
487491
pr-url: https://github.com/nodejs/node/pull/41003
488492
description: The `numericSeparator` option is supported now.
489493
- version:
@@ -582,8 +586,9 @@ changes:
582586
* `showProxy` {boolean} If `true`, `Proxy` inspection includes
583587
the [`target` and `handler`][] objects. **Default:** `false`.
584588
* `maxArrayLength` {integer} Specifies the maximum number of `Array`,
585-
[`TypedArray`][], [`WeakMap`][], and [`WeakSet`][] elements to include when
586-
formatting. Set to `null` or `Infinity` to show all elements. Set to `0` or
589+
[`TypedArray`][], [`Map`][], [`Set`][], [`WeakMap`][],
590+
and [`WeakSet`][] elements to include when formatting.
591+
Set to `null` or `Infinity` to show all elements. Set to `0` or
587592
negative to show no elements. **Default:** `100`.
588593
* `maxStringLength` {integer} Specifies the maximum number of characters to
589594
include when formatting. Set to `null` or `Infinity` to show all elements.

lib/internal/util/inspect.js

+25-6
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,8 @@ function addNumericSeparatorEnd(integerString) {
15531553
`${result}${integerString.slice(i)}`;
15541554
}
15551555

1556+
const remainingText = (remaining) => `... ${remaining} more item${remaining > 1 ? 's' : ''}`;
1557+
15561558
function formatNumber(fn, number, numericSeparator) {
15571559
if (!numericSeparator) {
15581560
// Format -0 as '-0'. Checking `number === -0` won't distinguish 0 from -0.
@@ -1680,7 +1682,7 @@ function formatSpecialArray(ctx, value, recurseTimes, maxLength, output, i) {
16801682
output.push(ctx.stylize(message, 'undefined'));
16811683
}
16821684
} else if (remaining > 0) {
1683-
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
1685+
output.push(remainingText(remaining));
16841686
}
16851687
return output;
16861688
}
@@ -1718,7 +1720,7 @@ function formatArray(ctx, value, recurseTimes) {
17181720
output.push(formatProperty(ctx, value, recurseTimes, i, kArrayType));
17191721
}
17201722
if (remaining > 0)
1721-
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
1723+
output.push(remainingText(remaining));
17221724
return output;
17231725
}
17241726

@@ -1733,7 +1735,7 @@ function formatTypedArray(value, length, ctx, ignored, recurseTimes) {
17331735
output[i] = elementFormatter(ctx.stylize, value[i], ctx.numericSeparator);
17341736
}
17351737
if (remaining > 0) {
1736-
output[maxLength] = `... ${remaining} more item${remaining > 1 ? 's' : ''}`;
1738+
output[maxLength] = remainingText(remaining);
17371739
}
17381740
if (ctx.showHidden) {
17391741
// .buffer goes last, it's not a primitive like the others.
@@ -1755,22 +1757,40 @@ function formatTypedArray(value, length, ctx, ignored, recurseTimes) {
17551757
}
17561758

17571759
function formatSet(value, ctx, ignored, recurseTimes) {
1760+
const length = value.size;
1761+
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length);
1762+
const remaining = length - maxLength;
17581763
const output = [];
17591764
ctx.indentationLvl += 2;
1765+
let i = 0;
17601766
for (const v of value) {
1767+
if (i >= maxLength) break;
17611768
ArrayPrototypePush(output, formatValue(ctx, v, recurseTimes));
1769+
i++;
1770+
}
1771+
if (remaining > 0) {
1772+
ArrayPrototypePush(output, remainingText(remaining));
17621773
}
17631774
ctx.indentationLvl -= 2;
17641775
return output;
17651776
}
17661777

17671778
function formatMap(value, ctx, ignored, recurseTimes) {
1779+
const length = value.size;
1780+
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length);
1781+
const remaining = length - maxLength;
17681782
const output = [];
17691783
ctx.indentationLvl += 2;
1784+
let i = 0;
17701785
for (const { 0: k, 1: v } of value) {
1786+
if (i >= maxLength) break;
17711787
output.push(
17721788
`${formatValue(ctx, k, recurseTimes)} => ${formatValue(ctx, v, recurseTimes)}`
17731789
);
1790+
i++;
1791+
}
1792+
if (remaining > 0) {
1793+
ArrayPrototypePush(output, remainingText(remaining));
17741794
}
17751795
ctx.indentationLvl -= 2;
17761796
return output;
@@ -1793,8 +1813,7 @@ function formatSetIterInner(ctx, recurseTimes, entries, state) {
17931813
}
17941814
const remaining = entries.length - maxLength;
17951815
if (remaining > 0) {
1796-
ArrayPrototypePush(output,
1797-
`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
1816+
ArrayPrototypePush(output, remainingText(remaining));
17981817
}
17991818
return output;
18001819
}
@@ -1832,7 +1851,7 @@ function formatMapIterInner(ctx, recurseTimes, entries, state) {
18321851
}
18331852
ctx.indentationLvl -= 2;
18341853
if (remaining > 0) {
1835-
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
1854+
output.push(remainingText(remaining));
18361855
}
18371856
return output;
18381857
}

test/parallel/test-util-inspect.js

+3
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,7 @@ if (typeof Symbol !== 'undefined') {
11721172
{
11731173
assert.strictEqual(util.inspect(new Set()), 'Set(0) {}');
11741174
assert.strictEqual(util.inspect(new Set([1, 2, 3])), 'Set(3) { 1, 2, 3 }');
1175+
assert.strictEqual(util.inspect(new Set([1, 2, 3]), { maxArrayLength: 1 }), 'Set(3) { 1, ... 2 more items }');
11751176
const set = new Set(['foo']);
11761177
set.bar = 42;
11771178
assert.strictEqual(
@@ -1192,6 +1193,8 @@ if (typeof Symbol !== 'undefined') {
11921193
assert.strictEqual(util.inspect(new Map()), 'Map(0) {}');
11931194
assert.strictEqual(util.inspect(new Map([[1, 'a'], [2, 'b'], [3, 'c']])),
11941195
"Map(3) { 1 => 'a', 2 => 'b', 3 => 'c' }");
1196+
assert.strictEqual(util.inspect(new Map([[1, 'a'], [2, 'b'], [3, 'c']]), { maxArrayLength: 1 }),
1197+
"Map(3) { 1 => 'a', ... 2 more items }");
11951198
const map = new Map([['foo', null]]);
11961199
map.bar = 42;
11971200
assert.strictEqual(util.inspect(map, true),

0 commit comments

Comments
 (0)