util: truncate inspect array and typed array by jasnell · Pull Request #6334 · nodejs/node · GitHub
Skip to content
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
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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

const?

Copy link
Copy Markdown
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
Copy Markdown
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