Skip to content

Commit 5629a7c

Browse files
committed
util: add maxArrayLength option to Set and Map
1 parent 7cbcc4f commit 5629a7c

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

doc/api/util.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,9 @@ stream.write('With ES6');
485485
<!-- YAML
486486
added: v0.3.0
487487
changes:
488+
- version: REPLACEME
489+
pr-url: https://github.com/nodejs/node/pull/43576
490+
description: add support for `maxArrayLength` when inspecting `Set` and `Map`.
488491
- version:
489492
- v17.3.0
490493
- v16.14.0
@@ -586,8 +589,9 @@ changes:
586589
* `showProxy` {boolean} If `true`, `Proxy` inspection includes
587590
the [`target` and `handler`][] objects. **Default:** `false`.
588591
* `maxArrayLength` {integer} Specifies the maximum number of `Array`,
589-
[`TypedArray`][], [`WeakMap`][], and [`WeakSet`][] elements to include when
590-
formatting. Set to `null` or `Infinity` to show all elements. Set to `0` or
592+
[`TypedArray`][], [`Map`][], [`Set`][], [`WeakMap`][],
593+
and [`WeakSet`][] elements to include when formatting.
594+
Set to `null` or `Infinity` to show all elements. Set to `0` or
591595
negative to show no elements. **Default:** `100`.
592596
* `maxStringLength` {integer} Specifies the maximum number of characters to
593597
include when formatting. Set to `null` or `Infinity` to show all elements.

lib/internal/util/inspect.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,8 @@ function addNumericSeparatorEnd(integerString) {
14871487
`${result}${integerString.slice(i)}`;
14881488
}
14891489

1490+
const remainingText = (remaining) => `... ${remaining} more item${remaining > 1 ? 's' : ''}`;
1491+
14901492
function formatNumber(fn, number, numericSeparator) {
14911493
if (!numericSeparator) {
14921494
// Format -0 as '-0'. Checking `number === -0` won't distinguish 0 from -0.
@@ -1613,7 +1615,7 @@ function formatSpecialArray(ctx, value, recurseTimes, maxLength, output, i) {
16131615
output.push(ctx.stylize(message, 'undefined'));
16141616
}
16151617
} else if (remaining > 0) {
1616-
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
1618+
output.push(remainingText(remaining));
16171619
}
16181620
return output;
16191621
}
@@ -1651,7 +1653,7 @@ function formatArray(ctx, value, recurseTimes) {
16511653
output.push(formatProperty(ctx, value, recurseTimes, i, kArrayType));
16521654
}
16531655
if (remaining > 0)
1654-
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
1656+
output.push(remainingText(remaining));
16551657
return output;
16561658
}
16571659

@@ -1666,7 +1668,7 @@ function formatTypedArray(value, length, ctx, ignored, recurseTimes) {
16661668
output[i] = elementFormatter(ctx.stylize, value[i], ctx.numericSeparator);
16671669
}
16681670
if (remaining > 0) {
1669-
output[maxLength] = `... ${remaining} more item${remaining > 1 ? 's' : ''}`;
1671+
output[maxLength] = remainingText(remaining);
16701672
}
16711673
if (ctx.showHidden) {
16721674
// .buffer goes last, it's not a primitive like the others.
@@ -1688,22 +1690,40 @@ function formatTypedArray(value, length, ctx, ignored, recurseTimes) {
16881690
}
16891691

16901692
function formatSet(value, ctx, ignored, recurseTimes) {
1693+
const length = value.size;
1694+
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length);
1695+
const remaining = length - maxLength;
16911696
const output = [];
16921697
ctx.indentationLvl += 2;
1698+
let i = 0;
16931699
for (const v of value) {
1700+
if (i >= maxLength) break;
16941701
ArrayPrototypePush(output, formatValue(ctx, v, recurseTimes));
1702+
i++;
1703+
}
1704+
if (remaining > 0) {
1705+
ArrayPrototypePush(output, remainingText(remaining));
16951706
}
16961707
ctx.indentationLvl -= 2;
16971708
return output;
16981709
}
16991710

17001711
function formatMap(value, ctx, ignored, recurseTimes) {
1712+
const length = value.size;
1713+
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length);
1714+
const remaining = length - maxLength;
17011715
const output = [];
17021716
ctx.indentationLvl += 2;
1717+
let i = 0;
17031718
for (const { 0: k, 1: v } of value) {
1719+
if (i >= maxLength) break;
17041720
output.push(
17051721
`${formatValue(ctx, k, recurseTimes)} => ${formatValue(ctx, v, recurseTimes)}`
17061722
);
1723+
i++;
1724+
}
1725+
if (remaining > 0) {
1726+
ArrayPrototypePush(output, remainingText(remaining));
17071727
}
17081728
ctx.indentationLvl -= 2;
17091729
return output;
@@ -1726,8 +1746,7 @@ function formatSetIterInner(ctx, recurseTimes, entries, state) {
17261746
}
17271747
const remaining = entries.length - maxLength;
17281748
if (remaining > 0) {
1729-
ArrayPrototypePush(output,
1730-
`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
1749+
ArrayPrototypePush(output, remainingText(remaining));
17311750
}
17321751
return output;
17331752
}
@@ -1765,7 +1784,7 @@ function formatMapIterInner(ctx, recurseTimes, entries, state) {
17651784
}
17661785
ctx.indentationLvl -= 2;
17671786
if (remaining > 0) {
1768-
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
1787+
output.push(remainingText(remaining));
17691788
}
17701789
return output;
17711790
}

test/parallel/test-util-inspect.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,7 @@ if (typeof Symbol !== 'undefined') {
11711171
{
11721172
assert.strictEqual(util.inspect(new Set()), 'Set(0) {}');
11731173
assert.strictEqual(util.inspect(new Set([1, 2, 3])), 'Set(3) { 1, 2, 3 }');
1174+
assert.strictEqual(util.inspect(new Set([1, 2, 3]), { maxArrayLength: 1 }), 'Set(3) { 1, ... 2 more items }');
11741175
const set = new Set(['foo']);
11751176
set.bar = 42;
11761177
assert.strictEqual(
@@ -1191,6 +1192,8 @@ if (typeof Symbol !== 'undefined') {
11911192
assert.strictEqual(util.inspect(new Map()), 'Map(0) {}');
11921193
assert.strictEqual(util.inspect(new Map([[1, 'a'], [2, 'b'], [3, 'c']])),
11931194
"Map(3) { 1 => 'a', 2 => 'b', 3 => 'c' }");
1195+
assert.strictEqual(util.inspect(new Map([[1, 'a'], [2, 'b'], [3, 'c']]), { maxArrayLength: 1 }),
1196+
"Map(3) { 1 => 'a', ... 2 more items }");
11941197
const map = new Map([['foo', null]]);
11951198
map.bar = 42;
11961199
assert.strictEqual(util.inspect(map, true),

0 commit comments

Comments
 (0)