Skip to content

Commit 71ca6d7

Browse 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

3 files changed

Lines changed: 34 additions & 8 deletions

File tree

‎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
@@ -1553,6 +1553,8 @@ function addNumericSeparatorEnd(integerString) {
15531553
`${result}${integerString.slice(i)}`;
15541554
}
15551555

1556+
constremainingText=(remaining)=>`... ${remaining} more item${remaining>1 ? 's' : ''}`;
1557+
15561558
functionformatNumber(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
}elseif(remaining>0){
1682-
output.push(`... ${remaining} more item${remaining>1 ? 's' : ''}`);
1684+
output.push(remainingText(remaining));
16831685
}
16841686
returnoutput;
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
returnoutput;
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
functionformatSet(value,ctx,ignored,recurseTimes){
1759+
constlength=value.size;
1760+
constmaxLength=MathMin(MathMax(0,ctx.maxArrayLength),length);
1761+
constremaining=length-maxLength;
17571762
constoutput=[];
17581763
ctx.indentationLvl+=2;
1764+
leti=0;
17591765
for(constvofvalue){
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
returnoutput;
17641775
}
17651776

17661777
functionformatMap(value,ctx,ignored,recurseTimes){
1778+
constlength=value.size;
1779+
constmaxLength=MathMin(MathMax(0,ctx.maxArrayLength),length);
1780+
constremaining=length-maxLength;
17671781
constoutput=[];
17681782
ctx.indentationLvl+=2;
1783+
leti=0;
17691784
for(const{0: k,1: v}ofvalue){
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
returnoutput;
@@ -1792,8 +1812,7 @@ function formatSetIterInner(ctx, recurseTimes, entries, state) {
17921812
}
17931813
constremaining=entries.length-maxLength;
17941814
if(remaining>0){
1795-
ArrayPrototypePush(output,
1796-
`... ${remaining} more item${remaining>1 ? 's' : ''}`);
1815+
ArrayPrototypePush(output,remainingText(remaining));
17971816
}
17981817
returnoutput;
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
returnoutput;
18371856
}

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

Lines 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(newSet()),'Set(0) {}');
11741174
assert.strictEqual(util.inspect(newSet([1,2,3])),'Set(3) { 1, 2, 3 }');
1175+
assert.strictEqual(util.inspect(newSet([1,2,3]),{maxArrayLength: 1}),'Set(3) { 1, ... 2 more items }');
11751176
constset=newSet(['foo']);
11761177
set.bar=42;
11771178
assert.strictEqual(
@@ -1192,6 +1193,8 @@ if (typeof Symbol !== 'undefined') {
11921193
assert.strictEqual(util.inspect(newMap()),'Map(0) {}');
11931194
assert.strictEqual(util.inspect(newMap([[1,'a'],[2,'b'],[3,'c']])),
11941195
"Map(3) { 1 => 'a', 2 => 'b', 3 => 'c' }");
1196+
assert.strictEqual(util.inspect(newMap([[1,'a'],[2,'b'],[3,'c']]),{maxArrayLength: 1}),
1197+
"Map(3) { 1 => 'a', ... 2 more items }");
11951198
constmap=newMap([['foo',null]]);
11961199
map.bar=42;
11971200
assert.strictEqual(util.inspect(map,true),

0 commit comments

Comments
 (0)