Skip to content

Commit

Permalink
util: improve inspect() compact number mode
Browse files Browse the repository at this point in the history
This fixes a proportion calculation for lots of short array entries
with at least one bigger one that alone makes up for more than one
fifth of all other entries together.

PR-URL: #26984
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
BridgeAR committed Apr 3, 2019
1 parent 75007d6 commit 14b2db0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -878,26 +878,27 @@ function groupArrayElements(ctx, output) {
let totalLength = 0;
let maxLength = 0;
let i = 0;
const separatorSpace = 2; // Add 1 for the space and 1 for the separator.
const dataLen = new Array(output.length);
// 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++) {
const len = ctx.colors ? removeColors(output[i]).length : output[i].length;
dataLen[i] = len;
totalLength += len;
totalLength += len + separatorSpace;
if (maxLength < len)
maxLength = len;
}
// Add two to `maxLength` as we add a single whitespace character plus a comma
// in-between two entries.
const actualMax = maxLength + 2;
const actualMax = maxLength + separatorSpace;
// Check if at least three entries fit next to each other and prevent grouping
// of arrays that contains entries of very different length (i.e., if a single
// entry is longer than 1/5 of all other entries combined). Otherwise the
// space in-between small entries would be enormous.
if (actualMax * 3 + ctx.indentationLvl < ctx.breakLength &&
(totalLength / maxLength > 5 || maxLength <= 6)) {
(totalLength / actualMax > 5 || maxLength <= 6)) {

const approxCharHeights = 2.5;
const bias = 1;
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2121,6 +2121,30 @@ assert.strictEqual(

assert.strictEqual(out, expected);

obj = [
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 123456789
];

out = util.inspect(obj, { compact: 3 });

expected = [
'[',
' 1, 1, 1,',
' 1, 1, 1,',
' 1, 1, 1,',
' 1, 1, 1,',
' 1, 1, 1,',
' 1, 1, 1,',
' 1, 1, 1,',
' 1, 1, 1,',
' 1, 1, 123456789',
']'
].join('\n');

assert.strictEqual(out, expected);

// Verify that array grouping and line consolidation does not happen together.
obj = {
a: {
Expand Down

0 comments on commit 14b2db0

Please sign in to comment.