Skip to content

Commit

Permalink
util: fix inspect array w. negative maxArrayLength
Browse files Browse the repository at this point in the history
PR-URL: #14880
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Alexey Orlenko <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
  • Loading branch information
BridgeAR committed Aug 21, 2017
1 parent aa24777 commit 3a886ff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,11 +682,12 @@ function formatObject(ctx, value, recurseTimes, visibleKeys, keys) {


function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length);
var output = [];
let visibleLength = 0;
let index = 0;
for (const elem of keys) {
if (visibleLength === ctx.maxArrayLength)
if (visibleLength === maxLength)
break;
// Symbols might have been added to the keys
if (typeof elem !== 'string')
Expand All @@ -701,15 +702,15 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
const message = `<${emptyItems} empty item${ending}>`;
output.push(ctx.stylize(message, 'undefined'));
index = i;
if (++visibleLength === ctx.maxArrayLength)
if (++visibleLength === maxLength)
break;
}
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
elem, true));
visibleLength++;
index++;
}
if (index < value.length && visibleLength !== ctx.maxArrayLength) {
if (index < value.length && visibleLength !== maxLength) {
const len = value.length - index;
const ending = len > 1 ? 's' : '';
const message = `<${len} empty item${ending}>`;
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,10 @@ if (typeof Symbol !== 'undefined') {
{
const x = new Array(101).fill();
assert(!util.inspect(x, { maxArrayLength: 101 }).endsWith('1 more item ]'));
assert.strictEqual(
util.inspect(x, { maxArrayLength: -1 }),
'[ ... 101 more items ]'
);
}

{
Expand Down

0 comments on commit 3a886ff

Please sign in to comment.