Browse Source

util: fix inspect array w. negative maxArrayLength

PR-URL: https://github.com/nodejs/node/pull/14880
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
canary-base
Ruben Bridgewater 8 years ago
parent
commit
3a886ffa24
No known key found for this signature in database GPG Key ID: F07496B3EB3C1762
  1. 7
      lib/util.js
  2. 4
      test/parallel/test-util-inspect.js

7
lib/util.js

@ -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')
@ -701,7 +702,7 @@ 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,
@ -709,7 +710,7 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
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}>`;

4
test/parallel/test-util-inspect.js

@ -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 ]'
);
}
{

Loading…
Cancel
Save