From b6f113bc1514e9da2e619caf3f9d7799d42fd8cb Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 5 Jun 2019 14:57:46 +0200 Subject: [PATCH] util: use average bias while grouping arrays This makes sure that strongly deviating entry length are taken into account while grouping arrays. PR-URL: https://github.com/nodejs/node/pull/28070 Refs: https://github.com/nodejs/node/issues/27690 Reviewed-By: James M Snell --- lib/internal/util/inspect.js | 3 ++- test/parallel/test-util-inspect.js | 14 ++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index c018a73a93fe7f..756ddc551b14bc 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -972,7 +972,8 @@ function groupArrayElements(ctx, output, value) { (totalLength / actualMax > 5 || maxLength <= 6)) { const approxCharHeights = 2.5; - const biasedMax = Math.max(actualMax - 4, 1); + const averageBias = Math.sqrt(actualMax - totalLength / output.length); + const biasedMax = Math.max(actualMax - 3 - averageBias, 1); // Dynamically check how many columns seem possible. const columns = Math.min( // Ideally a square should be drawn. We expect a character to be about 2.5 diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 794b23b3ece458..f4f76a4981488f 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -2252,14 +2252,12 @@ assert.strictEqual( 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, 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');