Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: fix inspect array w. negative maxArrayLength #14880

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,11 +679,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 @@ -698,15 +699,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 @@ -960,6 +960,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