Skip to content

Commit c67df38

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

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
}
@@ -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

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy