From a881d0ede37698a3a5fdb189d31f6f4b5c215c73 Mon Sep 17 00:00:00 2001 From: Hamid Reza Ghavami Date: Mon, 6 Jul 2026 10:09:21 +0300 Subject: [PATCH] util: allow single-line format when break length is infinite Signed-off-by: Hamid Reza Ghavami --- lib/internal/util/inspect.js | 8 ++++++++ test/parallel/test-util-inspect.js | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index c959c056fece45..9eb8ea66e444cb 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -2624,6 +2624,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; diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 6a1da6d4129fbd..161090e30f2d6a 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -4058,3 +4058,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); +}