diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2017-12-09 05:27:21 -0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2017-12-21 03:45:25 -0300 |
commit | c2203cb4dd240a6644177f04d0b40f40090b4c33 (patch) | |
tree | 9e31254f84b72f5fb05178dcc30db112d507f8a5 /doc | |
parent | 2d374916ebbaaa83aaf68577b75d41c6bb6ca9b8 (diff) | |
download | android-node-v8-c2203cb4dd240a6644177f04d0b40f40090b4c33.tar.gz android-node-v8-c2203cb4dd240a6644177f04d0b40f40090b4c33.tar.bz2 android-node-v8-c2203cb4dd240a6644177f04d0b40f40090b4c33.zip |
util: add util.inspect compact option
The current default formatting is not ideal and this improves
the situation by formatting the output more intuitiv.
1) All object keys are now indented by 2 characters instead of
sometimes 2 and sometimes 3 characters.
2) Each object key will now use an individual line instead of
sharing a line potentially with multiple object keys.
3) Long strings will now be split into multiple lines in case
they exceed the "lineBreak" option length (including the
current indentation).
4) Opening braces are now directly behind a object property
instead of using a new line.
5) Switch inspect "base" order. In case the compact option is set
to `false`, inspect will now print
"[Function: foo] {\n property: 'data'\n}"
instead of
"{ [Function: foo]\n property: 'data'\n}".
PR-URL: https://github.com/nodejs/node/pull/17576
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'doc')
-rw-r--r-- | doc/api/util.md | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/doc/api/util.md b/doc/api/util.md index c653802d3d..a07ff2b169 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -322,6 +322,9 @@ stream.write('With ES6'); <!-- YAML added: v0.3.0 changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/REPLACEME + description: The `compact` option is supported now. - version: v6.6.0 pr-url: https://github.com/nodejs/node/pull/8174 description: Custom inspection functions can now return `this`. @@ -360,6 +363,13 @@ changes: * `breakLength` {number} The length at which an object's keys are split across multiple lines. Set to `Infinity` to format an object as a single line. Defaults to 60 for legacy compatibility. + * `compact` {boolean} Setting this to `false` changes the default indentation + to use a line break for each object key instead of lining up multiple + properties in one line. It will also break text that is above the + `breakLength` size into smaller and better readable chunks and indents + objects the same as arrays. Note that no text will be reduced below 16 + characters, no matter the `breakLength` size. For more information, see the + example below. Defaults to `true`. The `util.inspect()` method returns a string representation of `object` that is intended for debugging. The output of `util.inspect` may change at any time @@ -396,6 +406,63 @@ Values may supply their own custom `inspect(depth, opts)` functions, when called these receive the current `depth` in the recursive inspection, as well as the options object passed to `util.inspect()`. +The following example highlights the difference with the `compact` option: + +```js +const util = require('util'); + +const o = { + a: [1, 2, [[ + 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ' + + 'eiusmod tempor incididunt ut labore et dolore magna aliqua.', + 'test', + 'foo']], 4], + b: new Map([['za', 1], ['zb', 'test']]) +}; +console.log(util.inspect(o, { compact: true, depth: 5, breakLength: 80 })); + +// This will print + +// { a: +// [ 1, +// 2, +// [ [ 'Lorem ipsum dolor sit amet, consectetur [...]', // A long line +// 'test', +// 'foo' ] ], +// 4 ], +// b: Map { 'za' => 1, 'zb' => 'test' } } + +// Setting `compact` to false changes the output to be more reader friendly. +console.log(util.inspect(o, { compact: false, depth: 5, breakLength: 80 })); + +// { +// a: [ +// 1, +// 2, +// [ +// [ +// 'Lorem ipsum dolor sit amet, consectetur ' + +// 'adipiscing elit, sed do eiusmod tempor ' + +// 'incididunt ut labore et dolore magna ' + +// 'aliqua., +// 'test', +// 'foo' +// ] +// ], +// 4 +// ], +// b: Map { +// 'za' => 1, +// 'zb' => 'test' +// } +// } + +// Setting `breakLength` to e.g. 150 will print the "Lorem ipsum" text in a +// single line. +// Reducing the `breakLength` will split the "Lorem ipsum" text in smaller +// chunks. +``` + ### Customizing `util.inspect` colors <!-- type=misc --> |