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: truncate inspect array and typed array #6334

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
4 changes: 4 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ formatted string:
will be introspected to show their `target` and `hander` objects. Defaults to
`false`.

- `maxArrayLength` - specifies the maximum number of Array and TypedArray
elements to include when formatting. Defaults to `100`. Set to `null` to
show all array elements. Set to `0` or negative to show no array elements.

Example of inspecting all properties of the `util` object:

```js
Expand Down
19 changes: 16 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const internalUtil = require('internal/util');
const binding = process.binding('util');

const isError = internalUtil.isError;
const kDefaultMaxLength = 100;

var Debug;

Expand Down Expand Up @@ -141,6 +142,8 @@ function inspect(obj, opts) {
if (ctx.customInspect === undefined) ctx.customInspect = true;
if (ctx.showProxy === undefined) ctx.showProxy = false;
if (ctx.colors) ctx.stylize = stylizeWithColor;
if (ctx.maxArrayLength === undefined) ctx.maxArrayLength = kDefaultMaxLength;
if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity;
return formatValue(ctx, obj, ctx.depth);
}
exports.inspect = inspect;
Expand Down Expand Up @@ -579,14 +582,19 @@ function formatObject(ctx, value, recurseTimes, visibleKeys, keys) {

function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
var output = [];
for (var i = 0, l = value.length; i < l; ++i) {
const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length);
const remaining = value.length - maxLength;
for (var i = 0; i < maxLength; ++i) {
if (hasOwnProperty(value, String(i))) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
String(i), true));
} else {
output.push('');
}
}
if (remaining > 0) {
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
}
keys.forEach(function(key) {
if (typeof key === 'symbol' || !key.match(/^\d+$/)) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
Expand All @@ -598,9 +606,14 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {


function formatTypedArray(ctx, value, recurseTimes, visibleKeys, keys) {
var output = new Array(value.length);
for (var i = 0, l = value.length; i < l; ++i)
const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length);
const remaining = value.length - maxLength;
var output = new Array(maxLength);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept as var to remain consistent with every other instance of var output = in this file. Would rather maintain that consistency and fix those up all at once separately.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¯_(ツ)_/¯

for (var i = 0; i < maxLength; ++i)
output[i] = formatNumber(ctx, value[i]);
if (remaining > 0) {
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
}
for (const key of keys) {
if (typeof key === 'symbol' || !key.match(/^\d+$/)) {
output.push(
Expand Down
55 changes: 55 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,3 +648,58 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));
const x = Object.create(null);
assert.equal(util.inspect(x), '{}');
}

// The following maxArrayLength tests were introduced after v6.0.0 was released.
// Do not backport to v5/v4 unless all of
// https://github.com/nodejs/node/pull/6334 is backported.
{
const x = Array(101);
assert(/1 more item/.test(util.inspect(x)));
}

{
const x = Array(101);
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: 101})));
}

{
const x = Array(101);
assert(/^\[ ... 101 more items \]$/.test(
util.inspect(x, {maxArrayLength: 0})));
}

{
const x = new Uint8Array(101);
assert(/1 more item/.test(util.inspect(x)));
}

{
const x = new Uint8Array(101);
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: 101})));
}

{
const x = new Uint8Array(101);
assert(/\[ ... 101 more items \]$/.test(
util.inspect(x, {maxArrayLength: 0})));
}

{
const x = Array(101);
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: null})));
}

{
const x = Array(101);
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: Infinity})));
}

{
const x = new Uint8Array(101);
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: null})));
}

{
const x = new Uint8Array(101);
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: Infinity})));
}