Skip to content

Commit 3b8914d

Browse files
cjihrigFishrock123
authored andcommitted
util: add an option for configuring break length
This commit adds a breakLength option to util.inspect(). This option allows users to control the length at which object keys are split across multiple lines. For backwards compatibility, this option defaults to 60. Fixes: #7305 PR-URL: #7499 Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
1 parent 15a32dd commit 3b8914d

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

doc/api/util.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ stream.write('It works!'); // Received data: "It works!"
187187
`TypedArray` elements to include when formatting. Defaults to `100`. Set to
188188
`null` to show all array elements. Set to `0` or negative to show no array
189189
elements.
190+
* `breakLength` {number} The length at which an object's keys are split
191+
across multiple lines. Set to `Infinity` to format an object as a single
192+
line. Defaults to 60 for legacy compatibility.
190193

191194
The `util.inspect()` method returns a string representation of `object` that is
192195
primarily useful for debugging. Additional `options` may be passed that alter

lib/util.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ function inspect(obj, opts) {
194194
if (ctx.colors) ctx.stylize = stylizeWithColor;
195195
if (ctx.maxArrayLength === undefined) ctx.maxArrayLength = kDefaultMaxLength;
196196
if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity;
197+
if (ctx.breakLength === undefined) ctx.breakLength = 60;
197198
return formatValue(ctx, obj, ctx.depth);
198199
}
199200
exports.inspect = inspect;
@@ -575,7 +576,7 @@ function formatValue(ctx, value, recurseTimes) {
575576

576577
ctx.seen.pop();
577578

578-
return reduceToSingleString(output, base, braces);
579+
return reduceToSingleString(output, base, braces, ctx.breakLength);
579580
}
580581

581582

@@ -807,12 +808,12 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
807808
}
808809

809810

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

815-
if (length > 60) {
816+
if (length > breakLength) {
816817
return braces[0] +
817818
// If the opening "brace" is too large, like in the case of "Set {",
818819
// we need to force the first item to be on the next line or the

test/parallel/test-util-inspect.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,3 +709,16 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));
709709
const x = new Uint8Array(101);
710710
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: Infinity})));
711711
}
712+
713+
{
714+
const obj = {foo: 'abc', bar: 'xyz'};
715+
const oneLine = util.inspect(obj, {breakLength: Infinity});
716+
// Subtract four for the object's two curly braces and two spaces of padding.
717+
// Add one more to satisfy the strictly greater than condition in the code.
718+
const breakpoint = oneLine.length - 5;
719+
const twoLines = util.inspect(obj, {breakLength: breakpoint});
720+
721+
assert.strictEqual(oneLine, '{ foo: \'abc\', bar: \'xyz\' }');
722+
assert.strictEqual(oneLine, util.inspect(obj, {breakLength: breakpoint + 1}));
723+
assert.strictEqual(twoLines, '{ foo: \'abc\',\n bar: \'xyz\' }');
724+
}

0 commit comments

Comments
 (0)