util: allow single-line format when break length is infinite

Signed-off-by: Hamid Reza Ghavami <hamidr.ghavami@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/64238
Reviewed-By: Jordan Harband <ljharb@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
This commit is contained in:
Hamid Reza Ghavami authored and GitHub committed 2026-08-24 14:31:34 +00:00
1 parent 278ead184f
commit f509cf1d08
2 files changed
+14

No files matched your search

+8
View File
@@ -2622,6 +2622,14 @@ function isBelowBreakLength(ctx, output, start, base) {
// TODO(BridgeAR): Add unicode support. Use the readline getStringWidth
// function. Check the performance overhead and make it an opt-in in case it's
// significant.
// allow the single-line format if the length limit is infinite and no items have newlines
if (ctx.breakLength === Infinity) {
if (base !== '' && StringPrototypeIncludes(base, '\n')) return false;
for (let i = 0; i < output.length; i++) {
if (typeof output[i] === 'string' && StringPrototypeIncludes(output[i], '\n')) return false;
}
return true;
}
let totalLength = output.length + start;
if (totalLength + output.length > ctx.breakLength)
return false;
+6
View File
@@ -4064,3 +4064,9 @@ ${error.stack.split('\n').slice(1).join('\n')}`,
assert.match(inspect(DOMException.prototype), /^\[object DOMException\] \{/);
delete Error[Symbol.hasInstance];
}
{
const obj = { a: 'short string', b: [1, 2], c: { d: true } };
const expected = "{ a: 'short string', b: [ 1, 2 ], c: { d: true } }";
assert.strictEqual(util.inspect(obj, { breakLength: Infinity }), expected);
}