Skip to content

Commit 572766a

Browse files
committed
util: add maxArrayLength option to Set and Map
1 parent 3507b3f commit 572766a

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

doc/api/util.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,9 @@ changes:
586586
* `showProxy` {boolean} If `true`, `Proxy` inspection includes
587587
the [`target` and `handler`][] objects. **Default:** `false`.
588588
* `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
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
591592
negative to show no elements. **Default:** `100`.
592593
* `maxStringLength` {integer} Specifies the maximum number of characters to
593594
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
@@ -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
}
@@ -1650,7 +1652,7 @@ function formatArray(ctx, value, recurseTimes) {
16501652
output.push(formatProperty(ctx, value, recurseTimes, i, kArrayType));
16511653
}
16521654
if (remaining > 0)
1653-
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
1655+
output.push(remainingText(remaining));
16541656
return output;
16551657
}
16561658

@@ -1665,7 +1667,7 @@ function formatTypedArray(value, length, ctx, ignored, recurseTimes) {
16651667
output[i] = elementFormatter(ctx.stylize, value[i], ctx.numericSeparator);
16661668
}
16671669
if (remaining > 0) {
1668-
output[maxLength] = `... ${remaining} more item${remaining > 1 ? 's' : ''}`;
1670+
output[maxLength] = remainingText(remaining);
16691671
}
16701672
if (ctx.showHidden) {
16711673
// .buffer goes last, it's not a primitive like the others.
@@ -1687,22 +1689,40 @@ function formatTypedArray(value, length, ctx, ignored, recurseTimes) {
16871689
}
16881690

16891691
function formatSet(value, ctx, ignored, recurseTimes) {
1692+
const length = value.size;
1693+
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length);
1694+
const remaining = length - maxLength;
16901695
const output = [];
16911696
ctx.indentationLvl += 2;
1697+
let i = 0;
16921698
for (const v of value) {
1699+
if (i >= maxLength) break;
16931700
ArrayPrototypePush(output, formatValue(ctx, v, recurseTimes));
1701+
i++;
1702+
}
1703+
if (remaining > 0) {
1704+
ArrayPrototypePush(output, remainingText(remaining));
16941705
}
16951706
ctx.indentationLvl -= 2;
16961707
return output;
16971708
}
16981709

16991710
function formatMap(value, ctx, ignored, recurseTimes) {
1711+
const length = value.size;
1712+
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length);
1713+
const remaining = length - maxLength;
17001714
const output = [];
17011715
ctx.indentationLvl += 2;
1716+
let i = 0;
17021717
for (const { 0: k, 1: v } of value) {
1718+
if (i >= maxLength) break;
17031719
output.push(
17041720
`${formatValue(ctx, k, recurseTimes)} => ${formatValue(ctx, v, recurseTimes)}`
17051721
);
1722+
i++;
1723+
}
1724+
if (remaining > 0) {
1725+
ArrayPrototypePush(output, remainingText(remaining));
17061726
}
17071727
ctx.indentationLvl -= 2;
17081728
return output;
@@ -1725,8 +1745,7 @@ function formatSetIterInner(ctx, recurseTimes, entries, state) {
17251745
}
17261746
const remaining = entries.length - maxLength;
17271747
if (remaining > 0) {
1728-
ArrayPrototypePush(output,
1729-
`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
1748+
ArrayPrototypePush(output, remainingText(remaining));
17301749
}
17311750
return output;
17321751
}
@@ -1764,7 +1783,7 @@ function formatMapIterInner(ctx, recurseTimes, entries, state) {
17641783
}
17651784
ctx.indentationLvl -= 2;
17661785
if (remaining > 0) {
1767-
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
1786+
output.push(remainingText(remaining));
17681787
}
17691788
return output;
17701789
}

test/parallel/test-util-inspect.js

+3
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)