Skip to content

Commit c3243de

Browse files
committed
util: special handle maxArrayLength while grouping arrays
This makes sure that large arrays with lots of small entries ignore the `... n more item(s)` part since it often resulted in output that users did not expect. Now that part is printed on a separate line to indicate extra entries. PR-URL: #28059 Refs: #27690 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent a5fdedb commit c3243de

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

‎lib/internal/util/inspect.js‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -946,12 +946,17 @@ function groupArrayElements(ctx, output){
946946
lettotalLength=0;
947947
letmaxLength=0;
948948
leti=0;
949+
letoutputLength=output.length;
950+
if(ctx.maxArrayLength<output.length){
951+
// This makes sure the "... n more items" part is not taken into account.
952+
outputLength--;
953+
}
949954
constseparatorSpace=2;// Add 1 for the space and 1 for the separator.
950-
constdataLen=newArray(output.length);
955+
constdataLen=newArray(outputLength);
951956
// Calculate the total length of all output entries and the individual max
952957
// entries length of all output entries. We have to remove colors first,
953958
// otherwise the length would not be calculated properly.
954-
for(;i<output.length;i++){
959+
for(;i<outputLength;i++){
955960
constlen=ctx.colors ? removeColors(output[i]).length : output[i].length;
956961
dataLen[i]=len;
957962
totalLength+=len+separatorSpace;
@@ -979,7 +984,7 @@ function groupArrayElements(ctx, output){
979984
// The added bias slightly increases the columns for short entries.
980985
Math.round(
981986
Math.sqrt(
982-
approxCharHeights*(actualMax-bias)*output.length
987+
approxCharHeights*(actualMax-bias)*outputLength
983988
)/(actualMax-bias)
984989
),
985990
// Do not exceed the breakLength.
@@ -1003,20 +1008,23 @@ function groupArrayElements(ctx, output){
10031008
firstLineMaxLength=dataLen[i];
10041009
}
10051010
// Each iteration creates a single line of grouped entries.
1006-
for(i=0;i<output.length;i+=columns){
1011+
for(i=0;i<outputLength;i+=columns){
10071012
// Calculate extra color padding in case it's active. This has to be done
10081013
// line by line as some lines might contain more colors than others.
10091014
letcolorPadding=output[i].length-dataLen[i];
10101015
// Add padding to the first column of the output.
10111016
letstr=output[i].padStart(firstLineMaxLength+colorPadding,' ');
10121017
// The last lines may contain less entries than columns.
1013-
constmax=Math.min(i+columns,output.length);
1018+
constmax=Math.min(i+columns,outputLength);
10141019
for(varj=i+1;j<max;j++){
10151020
colorPadding=output[j].length-dataLen[j];
10161021
str+=`, ${output[j].padStart(maxLength+colorPadding,' ')}`;
10171022
}
10181023
tmp.push(str);
10191024
}
1025+
if(ctx.maxArrayLength<output.length){
1026+
tmp.push(output[outputLength]);
1027+
}
10201028
output=tmp;
10211029
}
10221030
returnoutput;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,7 +2175,7 @@ assert.strictEqual(
21752175
[1,2,{a: 1,b: 2,c: 3}]
21762176
],
21772177
c: ['foo',4,444444],
2178-
d: Array.from({length: 100}).map((e,i)=>{
2178+
d: Array.from({length: 101}).map((e,i)=>{
21792179
returni%2===0 ? i*i : i;
21802180
}),
21812181
e: Array(6).fill('foobar'),
@@ -2224,7 +2224,8 @@ assert.strictEqual(
22242224
' 77, 6084, 79, 6400, 81, 6724, 83,',
22252225
' 7056, 85, 7396, 87, 7744, 89, 8100,',
22262226
' 91, 8464, 93, 8836, 95, 9216, 97,',
2227-
' 9604, 99',
2227+
' 9604, 99,',
2228+
' ... 1 more item',
22282229
' ],',
22292230
' e: [',
22302231
" 'foobar',",

0 commit comments

Comments
(0)