Skip to content

util: add an option for configuring break length #7499

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

Merged
merged 1 commit into from
Jul 5, 2016
Merged
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
3 changes: 3 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ stream.write('It works!'); // Received data: "It works!"
`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.
* `breakLength` {number} The length at which an object's keys are split
across multiple lines. Set to `Infinity` to format an object as a single
line. Defaults to 60 for legacy compatibility.

The `util.inspect()` method returns a string representation of `object` that is
primarily useful for debugging. Additional `options` may be passed that alter
Expand Down
7 changes: 4 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ function inspect(obj, opts) {
if (ctx.colors) ctx.stylize = stylizeWithColor;
if (ctx.maxArrayLength === undefined) ctx.maxArrayLength = kDefaultMaxLength;
if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity;
if (ctx.breakLength === undefined) ctx.breakLength = 60;
Copy link
Member

Choose a reason for hiding this comment

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

what about setting to null or Infinity... what affect would that need to have? Just thinking about consistency with other options

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I picked 60 to maintain backwards compatibility. I could assign it to null or Infinity and then change the value later, but it seems like that would only complicate things.

Copy link
Member

Choose a reason for hiding this comment

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

I think 60 is fine, but if set to null or Infinity explicitly we should figure out what the behavior should be.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Infinity is documented to make the object print on a single line with no breaks. null, or any value for that matter, will be plugged in for breakLength in the length > breakLength comparison.

return formatValue(ctx, obj, ctx.depth);
}
exports.inspect = inspect;
Expand Down Expand Up @@ -575,7 +576,7 @@ function formatValue(ctx, value, recurseTimes) {

ctx.seen.pop();

return reduceToSingleString(output, base, braces);
return reduceToSingleString(output, base, braces, ctx.breakLength);
}


Expand Down Expand Up @@ -807,12 +808,12 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
}


function reduceToSingleString(output, base, braces) {
function reduceToSingleString(output, base, braces, breakLength) {
var length = output.reduce(function(prev, cur) {
return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
}, 0);

if (length > 60) {
if (length > breakLength) {
return braces[0] +
// If the opening "brace" is too large, like in the case of "Set {",
// we need to force the first item to be on the next line or the
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,16 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));
const x = new Uint8Array(101);
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: Infinity})));
}

{
const obj = {foo: 'abc', bar: 'xyz'};
const oneLine = util.inspect(obj, {breakLength: Infinity});
// Subtract four for the object's two curly braces and two spaces of padding.
// Add one more to satisfy the strictly greater than condition in the code.
const breakpoint = oneLine.length - 5;
const twoLines = util.inspect(obj, {breakLength: breakpoint});

assert.strictEqual(oneLine, '{ foo: \'abc\', bar: \'xyz\' }');
assert.strictEqual(oneLine, util.inspect(obj, {breakLength: breakpoint + 1}));
assert.strictEqual(twoLines, '{ foo: \'abc\',\n bar: \'xyz\' }');
}