Skip to content

Commit c9fece3

Browse files
committed
util: change inspect compact and breakLength default
This changes the `compact` default from `true` to `3`. That mode changes arrays to be grouped together, it alignes multiple small entries on a single line in similar to `compact` true but only for the most inner three depth levels and the closing brackets are always on the same indentation as the openeing of the object instead of at the same line as another property. Big strings will be naturally broken into multiple lines instead of having one huge line that is not well readable. The output size mainly stays the same that way while it will be smaller in case of big arrays. Increasing the `breakLength` to 80 adjusts for most terminals that support at least 80 characters in a single line and improves the general output that way. A lot of calculations use the `breakLength` to determine the concrete behavior. PR-URL: #27109 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 892c51f commit c9fece3

13 files changed

+198
-162
lines changed

doc/api/util.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,10 @@ stream.write('With ES6');
390390
<!-- YAML
391391
added: v0.3.0
392392
changes:
393+
- version: REPLACEME
394+
pr-url: https://github.com/nodejs/node/pull/27109
395+
description: The `compact` options default is changed to `3` and the
396+
`breakLength` options default is changed to `80`.
393397
- version: v11.11.0
394398
pr-url: https://github.com/nodejs/node/pull/26269
395399
description: The `compact` option accepts numbers for a new output mode.
@@ -463,16 +467,17 @@ changes:
463467
[`TypedArray`][], [`WeakMap`][] and [`WeakSet`][] elements to include when
464468
formatting. Set to `null` or `Infinity` to show all elements. Set to `0` or
465469
negative to show no elements. **Default:** `100`.
466-
* `breakLength` {integer} The length at which an object's keys are split
467-
across multiple lines. Set to `Infinity` to format an object as a single
468-
line. **Default:** `60` for legacy compatibility.
470+
* `breakLength` {integer} The length at which input values are split across
471+
multiple lines. Set to `Infinity` to format the input as a single line
472+
(in combination with `compact` set to `true` or any number >= `1`).
473+
**Default:** `80`.
469474
* `compact` {boolean|integer} Setting this to `false` causes each object key
470475
to be displayed on a new line. It will also add new lines to text that is
471476
longer than `breakLength`. If set to a number, the most `n` inner elements
472477
are united on a single line as long as all properties fit into
473478
`breakLength`. Short array elements are also grouped together. Note that no
474479
text will be reduced below 16 characters, no matter the `breakLength` size.
475-
For more information, see the example below. **Default:** `true`.
480+
For more information, see the example below. **Default:** `3`.
476481
* `sorted` {boolean|Function} If set to `true` or a function, all properties
477482
of an object, and `Set` and `Map` entries are sorted in the resulting
478483
string. If set to `true` the [default sort][] is used. If set to a function,

lib/internal/util/inspect.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ const inspectDefaultOptions = Object.seal({
9595
customInspect: true,
9696
showProxy: false,
9797
maxArrayLength: 100,
98-
breakLength: 60,
99-
compact: true,
98+
breakLength: 80,
99+
compact: 3,
100100
sorted: false,
101101
getters: false
102102
});

test/parallel/test-console-group.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,13 @@ function teardown() {
122122
const expectedOut = 'not indented\n' +
123123
' indented\n' +
124124
' also indented\n' +
125-
" { also: 'a',\n" +
125+
' {\n' +
126+
" also: 'a',\n" +
126127
" multiline: 'object',\n" +
127128
" should: 'be',\n" +
128129
" indented: 'properly',\n" +
129-
" kthx: 'bai' }\n";
130+
" kthx: 'bai'\n" +
131+
' }\n';
130132
const expectedErr = '';
131133

132134
c.log('not indented');

test/parallel/test-console.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,11 @@ for (const expected of expectedStrings) {
234234
}
235235

236236
assert.strictEqual(strings.shift(),
237-
"{ foo: 'bar',\n [Symbol(nodejs.util.inspect.custom)]: " +
238-
'[Function: [nodejs.util.inspect.custom]] }\n');
237+
"{\n foo: 'bar',\n [Symbol(nodejs.util.inspect.custom)]:" +
238+
' [Function: [nodejs.util.inspect.custom]]\n}\n');
239239
assert.strictEqual(strings.shift(),
240-
"{ foo: 'bar',\n [Symbol(nodejs.util.inspect.custom)]: " +
241-
'[Function: [nodejs.util.inspect.custom]] }\n');
240+
"{\n foo: 'bar',\n [Symbol(nodejs.util.inspect.custom)]:" +
241+
' [Function: [nodejs.util.inspect.custom]]\n}\n');
242242
assert.ok(strings.shift().includes('foo: [Object]'));
243243
assert.strictEqual(strings.shift().includes('baz'), false);
244244
assert.strictEqual(strings.shift(), 'inspect inspect\n');

test/parallel/test-http2-stream-client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const server = http2.createServer();
1111
server.on('stream', common.mustCall((stream) => {
1212
assert.strictEqual(stream.aborted, false);
1313
const insp = util.inspect(stream);
14-
assert.ok(/Http2Stream { id/.test(insp));
14+
assert.ok(/Http2Stream {/.test(insp));
1515
assert.ok(/ state:/.test(insp));
1616
assert.ok(/ readableState:/.test(insp));
1717
assert.ok(/ writableState:/.test(insp));

test/parallel/test-repl-pretty-stack.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function run({ command, expected, ...extraREPLOptions }) {
3030

3131
const tests = [
3232
{
33-
// test .load for a file that throws
33+
// Test .load for a file that throws.
3434
command: `.load ${fixtures.path('repl-pretty-stack.js')}`,
3535
expected: 'Thrown:\nError: Whoops!\n at repl:9:24\n' +
3636
' at d (repl:12:3)\n at c (repl:9:3)\n' +
@@ -48,20 +48,20 @@ const tests = [
4848
{
4949
command: '(() => { const err = Error(\'Whoops!\'); ' +
5050
'err.foo = \'bar\'; throw err; })()',
51-
expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22\n foo: \'bar\' }\n',
51+
expected: "Thrown:\nError: Whoops!\n at repl:1:22 {\n foo: 'bar'\n}\n",
5252
},
5353
{
5454
command: '(() => { const err = Error(\'Whoops!\'); ' +
5555
'err.foo = \'bar\'; throw err; })()',
56-
expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22\n foo: ' +
57-
"\u001b[32m'bar'\u001b[39m }\n",
56+
expected: 'Thrown:\nError: Whoops!\n at repl:1:22 {\n foo: ' +
57+
"\u001b[32m'bar'\u001b[39m\n}\n",
5858
useColors: true
5959
},
6060
{
6161
command: 'foo = bar;',
6262
expected: 'Thrown:\nReferenceError: bar is not defined\n'
6363
},
64-
// test anonymous IIFE
64+
// Test anonymous IIFE.
6565
{
6666
command: '(function() { throw new Error(\'Whoops!\'); })()',
6767
expected: 'Thrown:\nError: Whoops!\n at repl:1:21\n'

test/parallel/test-repl-underscore.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,13 @@ function testError() {
179179

180180
// The sync error, with individual property echoes
181181
'Thrown:',
182-
/^{ Error: ENOENT: no such file or directory, scandir '.*nonexistent.*'/,
182+
/^Error: ENOENT: no such file or directory, scandir '.*nonexistent\?'/,
183183
/Object\.readdirSync/,
184184
/^ errno: -(2|4058),$/,
185185
" syscall: 'scandir',",
186186
" code: 'ENOENT',",
187-
" path: '/nonexistent?' }",
187+
" path: '/nonexistent?'",
188+
'}',
188189
"'ENOENT'",
189190
"'scandir'",
190191

test/parallel/test-repl.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -534,15 +534,16 @@ const errorTests = [
534534
send: 'require("internal/repl")',
535535
expect: [
536536
'Thrown:',
537-
/^{ Error: Cannot find module 'internal\/repl'/,
537+
/^Error: Cannot find module 'internal\/repl'/,
538538
/^Require stack:/,
539539
/^- <repl>/,
540540
/^ at .*/,
541541
/^ at .*/,
542542
/^ at .*/,
543543
/^ at .*/,
544544
" code: 'MODULE_NOT_FOUND',",
545-
" requireStack: [ '<repl>' ] }"
545+
" requireStack: [ '<repl>' ]",
546+
'}'
546547
]
547548
},
548549
// REPL should handle quotes within regexp literal in multiline mode

test/parallel/test-util-format.js

+55-40
Original file line numberDiff line numberDiff line change
@@ -165,59 +165,74 @@ assert.strictEqual(util.format('%o', 42), '42');
165165
assert.strictEqual(util.format('%o', 'foo'), '\'foo\'');
166166
assert.strictEqual(
167167
util.format('%o', obj),
168-
'{ foo: \'bar\',\n' +
168+
'{\n' +
169+
' foo: \'bar\',\n' +
169170
' foobar: 1,\n' +
170-
' func:\n' +
171-
' { [Function: func]\n' +
172-
' [length]: 0,\n' +
173-
' [name]: \'func\',\n' +
174-
' [prototype]: func { [constructor]: [Circular] } } }');
171+
' func: [Function: func] {\n' +
172+
' [length]: 0,\n' +
173+
' [name]: \'func\',\n' +
174+
' [prototype]: func { [constructor]: [Circular] }\n' +
175+
' }\n' +
176+
'}');
175177
assert.strictEqual(
176178
util.format('%o', nestedObj2),
177-
'{ foo: \'bar\',\n' +
179+
'{\n' +
180+
' foo: \'bar\',\n' +
178181
' foobar: 1,\n' +
179-
' func:\n' +
180-
' [ { a:\n' +
181-
' { [Function: a]\n' +
182-
' [length]: 0,\n' +
183-
' [name]: \'a\',\n' +
184-
' [prototype]: a { [constructor]: [Circular] } } },\n' +
185-
' [length]: 1 ] }');
182+
' func: [\n' +
183+
' {\n' +
184+
' a: [Function: a] {\n' +
185+
' [length]: 0,\n' +
186+
' [name]: \'a\',\n' +
187+
' [prototype]: a { [constructor]: [Circular] }\n' +
188+
' }\n' +
189+
' },\n' +
190+
' [length]: 1\n' +
191+
' ]\n' +
192+
'}');
186193
assert.strictEqual(
187194
util.format('%o', nestedObj),
188-
'{ foo: \'bar\',\n' +
189-
' foobar:\n' +
190-
' { foo: \'bar\',\n' +
191-
' func:\n' +
192-
' { [Function: func]\n' +
193-
' [length]: 0,\n' +
194-
' [name]: \'func\',\n' +
195-
' [prototype]: func { [constructor]: [Circular] } } } }');
195+
'{\n' +
196+
' foo: \'bar\',\n' +
197+
' foobar: {\n' +
198+
' foo: \'bar\',\n' +
199+
' func: [Function: func] {\n' +
200+
' [length]: 0,\n' +
201+
' [name]: \'func\',\n' +
202+
' [prototype]: func { [constructor]: [Circular] }\n' +
203+
' }\n' +
204+
' }\n' +
205+
'}');
196206
assert.strictEqual(
197207
util.format('%o %o', obj, obj),
198-
'{ foo: \'bar\',\n' +
208+
'{\n' +
209+
' foo: \'bar\',\n' +
199210
' foobar: 1,\n' +
200-
' func:\n' +
201-
' { [Function: func]\n' +
202-
' [length]: 0,\n' +
203-
' [name]: \'func\',\n' +
204-
' [prototype]: func { [constructor]: [Circular] } } }' +
205-
' { foo: \'bar\',\n' +
211+
' func: [Function: func] {\n' +
212+
' [length]: 0,\n' +
213+
' [name]: \'func\',\n' +
214+
' [prototype]: func { [constructor]: [Circular] }\n' +
215+
' }\n' +
216+
'} {\n' +
217+
' foo: \'bar\',\n' +
206218
' foobar: 1,\n' +
207-
' func:\n' +
208-
' { [Function: func]\n' +
209-
' [length]: 0,\n' +
210-
' [name]: \'func\',\n' +
211-
' [prototype]: func { [constructor]: [Circular] } } }');
219+
' func: [Function: func] {\n' +
220+
' [length]: 0,\n' +
221+
' [name]: \'func\',\n' +
222+
' [prototype]: func { [constructor]: [Circular] }\n' +
223+
' }\n' +
224+
'}');
212225
assert.strictEqual(
213226
util.format('%o %o', obj),
214-
'{ foo: \'bar\',\n' +
227+
'{\n' +
228+
' foo: \'bar\',\n' +
215229
' foobar: 1,\n' +
216-
' func:\n' +
217-
' { [Function: func]\n' +
218-
' [length]: 0,\n' +
219-
' [name]: \'func\',\n' +
220-
' [prototype]: func { [constructor]: [Circular] } } } %o');
230+
' func: [Function: func] {\n' +
231+
' [length]: 0,\n' +
232+
' [name]: \'func\',\n' +
233+
' [prototype]: func { [constructor]: [Circular] }\n' +
234+
' }\n' +
235+
'} %o');
221236

222237
assert.strictEqual(util.format('%O'), '%O');
223238
assert.strictEqual(util.format('%O', 42), '42');

test/parallel/test-util-inspect-proxy.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ assert.strictEqual(
5151
util.inspect(proxyObj, opts),
5252
'Proxy [\n' +
5353
' [ 1, 2, 3 ],\n' +
54-
' { getPrototypeOf: [Function: getPrototypeOf],\n' +
54+
' {\n' +
55+
' getPrototypeOf: [Function: getPrototypeOf],\n' +
5556
' setPrototypeOf: [Function: setPrototypeOf],\n' +
5657
' isExtensible: [Function: isExtensible],\n' +
5758
' preventExtensions: [Function: preventExtensions],\n' +
@@ -63,7 +64,9 @@ assert.strictEqual(
6364
' deleteProperty: [Function: deleteProperty],\n' +
6465
' ownKeys: [Function: ownKeys],\n' +
6566
' apply: [Function: apply],\n' +
66-
' construct: [Function: construct] } ]'
67+
' construct: [Function: construct]\n' +
68+
' }\n' +
69+
']'
6770
);
6871

6972
// Using getProxyDetails with non-proxy returns undefined
@@ -89,14 +92,18 @@ const expected3 = 'Proxy [ Proxy [ Proxy [ {}, {} ], {} ], Proxy [ {}, {} ] ]';
8992
const expected4 = 'Proxy [ Proxy [ {}, {} ], Proxy [ Proxy [ {}, {} ], {} ] ]';
9093
const expected5 = 'Proxy [\n ' +
9194
'Proxy [ Proxy [ Proxy [Array], {} ], Proxy [ {}, {} ] ],\n' +
92-
' Proxy [ Proxy [ {}, {} ], Proxy [ Proxy [Array], {} ] ] ]';
95+
' Proxy [ Proxy [ {}, {} ], Proxy [ Proxy [Array], {} ] ]' +
96+
'\n]';
9397
const expected6 = 'Proxy [\n' +
9498
' Proxy [\n' +
9599
' Proxy [ Proxy [Array], Proxy [Array] ],\n' +
96-
' Proxy [ Proxy [Array], Proxy [Array] ] ],\n' +
100+
' Proxy [ Proxy [Array], Proxy [Array] ]\n' +
101+
' ],\n' +
97102
' Proxy [\n' +
98103
' Proxy [ Proxy [Array], Proxy [Array] ],\n' +
99-
' Proxy [ Proxy [Array], Proxy [Array] ] ] ]';
104+
' Proxy [ Proxy [Array], Proxy [Array] ]\n' +
105+
' ]\n' +
106+
']';
100107
assert.strictEqual(
101108
util.inspect(proxy1, { showProxy: true, depth: null }),
102109
expected1);

0 commit comments

Comments
 (0)