Skip to content

Commit 0059b87

Browse files
BridgeARantsmartian
authored andcommitted
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 <targos@protonmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 97a4246 commit 0059b87

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
@@ -971,12 +971,17 @@ function groupArrayElements(ctx, output) {
971971
let totalLength = 0;
972972
let maxLength = 0;
973973
let i = 0;
974+
let outputLength = output.length;
975+
if (ctx.maxArrayLength < output.length) {
976+
// This makes sure the "... n more items" part is not taken into account.
977+
outputLength--;
978+
}
974979
const separatorSpace = 2; // Add 1 for the space and 1 for the separator.
975-
const dataLen = new Array(output.length);
980+
const dataLen = new Array(outputLength);
976981
// Calculate the total length of all output entries and the individual max
977982
// entries length of all output entries. We have to remove colors first,
978983
// otherwise the length would not be calculated properly.
979-
for (; i < output.length; i++) {
984+
for (; i < outputLength; i++) {
980985
const len = ctx.colors ? removeColors(output[i]).length : output[i].length;
981986
dataLen[i] = len;
982987
totalLength += len + separatorSpace;
@@ -1004,7 +1009,7 @@ function groupArrayElements(ctx, output) {
10041009
// The added bias slightly increases the columns for short entries.
10051010
Math.round(
10061011
Math.sqrt(
1007-
approxCharHeights * (actualMax - bias) * output.length
1012+
approxCharHeights * (actualMax - bias) * outputLength
10081013
) / (actualMax - bias)
10091014
),
10101015
// Do not exceed the breakLength.
@@ -1028,20 +1033,23 @@ function groupArrayElements(ctx, output) {
10281033
firstLineMaxLength = dataLen[i];
10291034
}
10301035
// Each iteration creates a single line of grouped entries.
1031-
for (i = 0; i < output.length; i += columns) {
1036+
for (i = 0; i < outputLength; i += columns) {
10321037
// Calculate extra color padding in case it's active. This has to be done
10331038
// line by line as some lines might contain more colors than others.
10341039
let colorPadding = output[i].length - dataLen[i];
10351040
// Add padding to the first column of the output.
10361041
let str = output[i].padStart(firstLineMaxLength + colorPadding, ' ');
10371042
// The last lines may contain less entries than columns.
1038-
const max = Math.min(i + columns, output.length);
1043+
const max = Math.min(i + columns, outputLength);
10391044
for (var j = i + 1; j < max; j++) {
10401045
colorPadding = output[j].length - dataLen[j];
10411046
str += `, ${output[j].padStart(maxLength + colorPadding, ' ')}`;
10421047
}
10431048
tmp.push(str);
10441049
}
1050+
if (ctx.maxArrayLength < output.length) {
1051+
tmp.push(output[outputLength]);
1052+
}
10451053
output = tmp;
10461054
}
10471055
return output;

test/parallel/test-util-inspect.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,7 +2197,7 @@ assert.strictEqual(
21972197
[ 1, 2, { a: 1, b: 2, c: 3 } ]
21982198
],
21992199
c: ['foo', 4, 444444],
2200-
d: Array.from({ length: 100 }).map((e, i) => {
2200+
d: Array.from({ length: 101 }).map((e, i) => {
22012201
return i % 2 === 0 ? i * i : i;
22022202
}),
22032203
e: Array(6).fill('foobar'),
@@ -2246,7 +2246,8 @@ assert.strictEqual(
22462246
' 77, 6084, 79, 6400, 81, 6724, 83,',
22472247
' 7056, 85, 7396, 87, 7744, 89, 8100,',
22482248
' 91, 8464, 93, 8836, 95, 9216, 97,',
2249-
' 9604, 99',
2249+
' 9604, 99,',
2250+
' ... 1 more item',
22502251
' ],',
22512252
' e: [',
22522253
" 'foobar',",

0 commit comments

Comments
 (0)