Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 71ca6d7

Browse filesBrowse files
authored
util: add maxArrayLength option to Set and Map
PR-URL: #43576 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 48d4e3d commit 71ca6d7
Copy full SHA for 71ca6d7

File tree

Expand file treeCollapse file tree

3 files changed

+34
-8
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+34
-8
lines changed
Open diff view settings
Collapse file

‎doc/api/util.md‎

Copy file name to clipboardExpand all lines: doc/api/util.md
+6-2Lines changed: 6 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
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.
Collapse file

‎lib/internal/util/inspect.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js
+25-6Lines changed: 25 additions & 6 deletions
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.
@@ -1679,7 +1681,7 @@ function formatSpecialArray(ctx, value, recurseTimes, maxLength, output, i) {
16791681
output.push(ctx.stylize(message, 'undefined'));
16801682
}
16811683
} else if (remaining > 0) {
1682-
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
1684+
output.push(remainingText(remaining));
16831685
}
16841686
return output;
16851687
}
@@ -1717,7 +1719,7 @@ function formatArray(ctx, value, recurseTimes) {
17171719
output.push(formatProperty(ctx, value, recurseTimes, i, kArrayType));
17181720
}
17191721
if (remaining > 0)
1720-
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
1722+
output.push(remainingText(remaining));
17211723
return output;
17221724
}
17231725

@@ -1732,7 +1734,7 @@ function formatTypedArray(value, length, ctx, ignored, recurseTimes) {
17321734
output[i] = elementFormatter(ctx.stylize, value[i], ctx.numericSeparator);
17331735
}
17341736
if (remaining > 0) {
1735-
output[maxLength] = `... ${remaining} more item${remaining > 1 ? 's' : ''}`;
1737+
output[maxLength] = remainingText(remaining);
17361738
}
17371739
if (ctx.showHidden) {
17381740
// .buffer goes last, it's not a primitive like the others.
@@ -1754,22 +1756,40 @@ function formatTypedArray(value, length, ctx, ignored, recurseTimes) {
17541756
}
17551757

17561758
function formatSet(value, ctx, ignored, recurseTimes) {
1759+
const length = value.size;
1760+
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length);
1761+
const remaining = length - maxLength;
17571762
const output = [];
17581763
ctx.indentationLvl += 2;
1764+
let i = 0;
17591765
for (const v of value) {
1766+
if (i >= maxLength) break;
17601767
ArrayPrototypePush(output, formatValue(ctx, v, recurseTimes));
1768+
i++;
1769+
}
1770+
if (remaining > 0) {
1771+
ArrayPrototypePush(output, remainingText(remaining));
17611772
}
17621773
ctx.indentationLvl -= 2;
17631774
return output;
17641775
}
17651776

17661777
function formatMap(value, ctx, ignored, recurseTimes) {
1778+
const length = value.size;
1779+
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length);
1780+
const remaining = length - maxLength;
17671781
const output = [];
17681782
ctx.indentationLvl += 2;
1783+
let i = 0;
17691784
for (const { 0: k, 1: v } of value) {
1785+
if (i >= maxLength) break;
17701786
output.push(
17711787
`${formatValue(ctx, k, recurseTimes)} => ${formatValue(ctx, v, recurseTimes)}`
17721788
);
1789+
i++;
1790+
}
1791+
if (remaining > 0) {
1792+
ArrayPrototypePush(output, remainingText(remaining));
17731793
}
17741794
ctx.indentationLvl -= 2;
17751795
return output;
@@ -1792,8 +1812,7 @@ function formatSetIterInner(ctx, recurseTimes, entries, state) {
17921812
}
17931813
const remaining = entries.length - maxLength;
17941814
if (remaining > 0) {
1795-
ArrayPrototypePush(output,
1796-
`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
1815+
ArrayPrototypePush(output, remainingText(remaining));
17971816
}
17981817
return output;
17991818
}
@@ -1831,7 +1850,7 @@ function formatMapIterInner(ctx, recurseTimes, entries, state) {
18311850
}
18321851
ctx.indentationLvl -= 2;
18331852
if (remaining > 0) {
1834-
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
1853+
output.push(remainingText(remaining));
18351854
}
18361855
return output;
18371856
}
Collapse file

‎test/parallel/test-util-inspect.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js
+3Lines changed: 3 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.