Skip to content

Commit f509cf1

Browse files
util: allow single-line format when break length is infinite
Signed-off-by: Hamid Reza Ghavami <hamidr.ghavami@gmail.com> PR-URL: #64238 Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
1 parent 278ead1 commit f509cf1

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

lib/internal/util/inspect.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,6 +2622,14 @@ function isBelowBreakLength(ctx, output, start, base) {
26222622
// TODO(BridgeAR): Add unicode support. Use the readline getStringWidth
26232623
// function. Check the performance overhead and make it an opt-in in case it's
26242624
// significant.
2625+
// allow the single-line format if the length limit is infinite and no items have newlines
2626+
if (ctx.breakLength === Infinity) {
2627+
if (base !== '' && StringPrototypeIncludes(base, '\n')) return false;
2628+
for (let i = 0; i < output.length; i++) {
2629+
if (typeof output[i] === 'string' && StringPrototypeIncludes(output[i], '\n')) return false;
2630+
}
2631+
return true;
2632+
}
26252633
let totalLength = output.length + start;
26262634
if (totalLength + output.length > ctx.breakLength)
26272635
return false;

test/parallel/test-util-inspect.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4064,3 +4064,9 @@ ${error.stack.split('\n').slice(1).join('\n')}`,
40644064
assert.match(inspect(DOMException.prototype), /^\[object DOMException\] \{/);
40654065
delete Error[Symbol.hasInstance];
40664066
}
4067+
4068+
{
4069+
const obj = { a: 'short string', b: [1, 2], c: { d: true } };
4070+
const expected = "{ a: 'short string', b: [ 1, 2 ], c: { d: true } }";
4071+
assert.strictEqual(util.inspect(obj, { breakLength: Infinity }), expected);
4072+
}

0 commit comments

Comments
 (0)