diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2018-09-10 08:55:00 +0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-09-19 13:22:32 +0200 |
commit | b95b0d87c3582eee82600d076874c9d4afeb4b72 (patch) | |
tree | eb2c35eab92cd95f996f596b9f7616ddbefd56f3 /doc/api/util.md | |
parent | 9d71e6a6071134789a30471302d9735250dea96c (diff) | |
download | android-node-v8-b95b0d87c3582eee82600d076874c9d4afeb4b72.tar.gz android-node-v8-b95b0d87c3582eee82600d076874c9d4afeb4b72.tar.bz2 android-node-v8-b95b0d87c3582eee82600d076874c9d4afeb4b72.zip |
util: add order option to `.inspect()`
The order option can be used to sort the inspected values in case
they do not rely on their order as arrays. That way the output is
stable no matter of the object property inspection order.
PR-URL: https://github.com/nodejs/node/pull/22788
Refs: https://github.com/nodejs/node/issues/22763
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Diffstat (limited to 'doc/api/util.md')
-rw-r--r-- | doc/api/util.md | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/doc/api/util.md b/doc/api/util.md index 267bab85ae..b0e942274c 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -361,6 +361,9 @@ stream.write('With ES6'); added: v0.3.0 changes: - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/22788 + description: The `sorted` option is supported now. + - version: REPLACEME pr-url: https://github.com/nodejs/node/pull/22756 description: The inspection output is now limited to about 128 MB. Data above that size will not be fully inspected. @@ -426,6 +429,10 @@ changes: 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. **Default:** `true`. + * `sorted` {boolean|Function} If set to `true` or a function, all properties + of an object and Set and Map entries will be sorted in the returned string. + If set to `true` the [default sort][] is going to be used. If set to a + function, it is used as a [compare function][]. * Returns: {string} The representation of passed object The `util.inspect()` method returns a string representation of `object` that is @@ -535,6 +542,34 @@ console.log(inspect(weakSet, { showHidden: true })); // WeakSet { { a: 1 }, { b: 2 } } ``` +The `sorted` option makes sure the output is identical, no matter of the +properties insertion order: + +```js +const { inspect } = require('util'); +const assert = require('assert'); + +const o1 = { + b: [2, 3, 1], + a: '`a` comes before `b`', + c: new Set([2, 3, 1]) +}; +console.log(inspect(o1, { sorted: true })); +// { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set { 1, 2, 3 } } +console.log(inspect(o1, { sorted: (a, b) => a < b })); +// { c: Set { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' } + +const o2 = { + c: new Set([2, 1, 3]), + a: '`a` comes before `b`', + b: [2, 3, 1] +}; +assert.strict.equal( + inspect(o1, { sorted: true }), + inspect(o2, { sorted: true }) +); +``` + Please note that `util.inspect()` is a synchronous method that is mainly intended as a debugging tool. Its maximum output length is limited to approximately 128 MB and input values that result in output bigger than that @@ -2165,7 +2200,9 @@ Deprecated predecessor of `console.log`. [WHATWG Encoding Standard]: https://encoding.spec.whatwg.org/ [Common System Errors]: errors.html#errors_common_system_errors [async function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function +[compare function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Parameters [constructor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor +[default sort]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort [global symbol registry]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for [list of deprecated APIS]: deprecations.html#deprecations_list_of_deprecated_apis [semantically incompatible]: https://github.com/nodejs/node/issues/4179 |