Skip to content
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

util: use string width in util formatPrimitive() string length line check #43721

Closed
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
20 changes: 11 additions & 9 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1529,15 +1529,17 @@ function formatPrimitive(fn, value, ctx) {
value = value.slice(0, ctx.maxStringLength);
trailer = `... ${remaining} more character${remaining > 1 ? 's' : ''}`;
}
if (ctx.compact !== true &&
// TODO(BridgeAR): Add unicode support. Use the readline getStringWidth
// function.
value.length > kMinLineLength &&
value.length > ctx.breakLength - ctx.indentationLvl - 4) {
return value
.split(/(?<=\n)/)
.map((line) => fn(strEscape(line), 'string'))
.join(` +\n${' '.repeat(ctx.indentationLvl + 2)}`) + trailer;
if (ctx.compact !== true) {
const valueStringWidth = getStringWidth(value, ctx.colors);
if (
valueStringWidth > kMinLineLength &&
valueStringWidth > ctx.breakLength - ctx.indentationLvl - 4
) {
return value
.split(/(?<=\n)/)
.map((line) => fn(strEscape(line), 'string'))
.join(` +\n${' '.repeat(ctx.indentationLvl + 2)}`) + trailer;
}
}
return fn(strEscape(value), 'string') + trailer;
}
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2637,6 +2637,14 @@ assert.strictEqual(

assert.strictEqual(out, expected);

// Unicode character width support.
out = util.inspect(
`${('あ').repeat(5)}\n${('あ').repeat(5)}`,
{ compact: 3, breakLength: 15 }
);
expected = "'あああああ\\n' +\n 'あああああ'";
assert.strictEqual(out, expected);

// Array grouping should prevent lining up outer elements on a single line.
obj = [[[1, 2, 3, 4, 5, 6, 7, 8, 9]]];

Expand Down