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 c3243de

Browse filesBrowse 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 <targos@protonmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent a5fdedb commit c3243de
Copy full SHA for c3243de

File tree

Expand file treeCollapse file tree

2 files changed

+16
-7
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+16
-7
lines changed
Open diff view settings
Collapse file

‎lib/internal/util/inspect.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js
+13-5Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -946,12 +946,17 @@ function groupArrayElements(ctx, output) {
946946
let totalLength = 0;
947947
let maxLength = 0;
948948
let i = 0;
949+
let outputLength = 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
const separatorSpace = 2; // Add 1 for the space and 1 for the separator.
950-
const dataLen = new Array(output.length);
955+
const dataLen = new Array(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
const len = 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
let colorPadding = output[i].length - dataLen[i];
10101015
// Add padding to the first column of the output.
10111016
let str = output[i].padStart(firstLineMaxLength + colorPadding, ' ');
10121017
// The last lines may contain less entries than columns.
1013-
const max = Math.min(i + columns, output.length);
1018+
const max = Math.min(i + columns, outputLength);
10141019
for (var j = 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
return output;
Collapse file

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

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