diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2019-06-04 19:08:20 +0200 |
---|---|---|
committer | Anto Aravinth <anto.aravinth.cse@gmail.com> | 2019-06-09 14:35:31 +0530 |
commit | 0059b877e35eb933b2e544d1e33bccb16e05545b (patch) | |
tree | 88b3ef88330c68d088ba44f729f37d074dca1d9e /lib/internal/util/inspect.js | |
parent | 97a42465ab6f2e1cce33ca7a0d9cc6e8eea145f9 (diff) | |
download | android-node-v8-0059b877e35eb933b2e544d1e33bccb16e05545b.tar.gz android-node-v8-0059b877e35eb933b2e544d1e33bccb16e05545b.tar.bz2 android-node-v8-0059b877e35eb933b2e544d1e33bccb16e05545b.zip |
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: https://github.com/nodejs/node/pull/28059
Refs: https://github.com/nodejs/node/issues/27690
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'lib/internal/util/inspect.js')
-rw-r--r-- | lib/internal/util/inspect.js | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 4583929260..1292293cc6 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -971,12 +971,17 @@ function groupArrayElements(ctx, output) { let totalLength = 0; let maxLength = 0; let i = 0; + let outputLength = output.length; + if (ctx.maxArrayLength < output.length) { + // This makes sure the "... n more items" part is not taken into account. + outputLength--; + } const separatorSpace = 2; // Add 1 for the space and 1 for the separator. - const dataLen = new Array(output.length); + const dataLen = new Array(outputLength); // Calculate the total length of all output entries and the individual max // entries length of all output entries. We have to remove colors first, // otherwise the length would not be calculated properly. - for (; i < output.length; i++) { + for (; i < outputLength; i++) { const len = ctx.colors ? removeColors(output[i]).length : output[i].length; dataLen[i] = len; totalLength += len + separatorSpace; @@ -1004,7 +1009,7 @@ function groupArrayElements(ctx, output) { // The added bias slightly increases the columns for short entries. Math.round( Math.sqrt( - approxCharHeights * (actualMax - bias) * output.length + approxCharHeights * (actualMax - bias) * outputLength ) / (actualMax - bias) ), // Do not exceed the breakLength. @@ -1028,20 +1033,23 @@ function groupArrayElements(ctx, output) { firstLineMaxLength = dataLen[i]; } // Each iteration creates a single line of grouped entries. - for (i = 0; i < output.length; i += columns) { + for (i = 0; i < outputLength; i += columns) { // Calculate extra color padding in case it's active. This has to be done // line by line as some lines might contain more colors than others. let colorPadding = output[i].length - dataLen[i]; // Add padding to the first column of the output. let str = output[i].padStart(firstLineMaxLength + colorPadding, ' '); // The last lines may contain less entries than columns. - const max = Math.min(i + columns, output.length); + const max = Math.min(i + columns, outputLength); for (var j = i + 1; j < max; j++) { colorPadding = output[j].length - dataLen[j]; str += `, ${output[j].padStart(maxLength + colorPadding, ' ')}`; } tmp.push(str); } + if (ctx.maxArrayLength < output.length) { + tmp.push(output[outputLength]); + } output = tmp; } return output; |