@@ -360,6 +360,9 @@ stream.write('With ES6');
360360<!-- YAML
361361added: v0.3.0
362362changes:
363+ - version: REPLACEME
364+ pr-url: https://github.com/nodejs/node/pull/22788
365+ description: The `sorted` option is supported now.
363366 - version: v10.6.0
364367 pr-url: https://github.com/nodejs/node/pull/20725
365368 description: Inspecting linked lists and similar objects is now possible
@@ -420,7 +423,10 @@ changes:
420423 objects the same as arrays. Note that no text will be reduced below 16
421424 characters, no matter the ` breakLength ` size. For more information, see the
422425 example below. ** Default:** ` true ` .
423-
426+ * ` sorted ` {boolean|Function} If set to ` true ` or a function, all properties
427+ of an object and Set and Map entries will be sorted in the returned string.
428+ If set to ` true ` the [ default sort] [ ] is going to be used. If set to a
429+ function, it is used as a [ compare function] [ ] .
424430* Returns: {string} The representation of passed object
425431
426432The ` util.inspect() ` method returns a string representation of ` object ` that is
@@ -534,6 +540,34 @@ console.log(inspect(weakSet, { showHidden: true }));
534540// WeakSet { { a: 1 }, { b: 2 } }
535541```
536542
543+ The ` sorted ` option makes sure the output is identical, no matter of the
544+ properties insertion order:
545+
546+ ``` js
547+ const { inspect } = require (' util' );
548+ const assert = require (' assert' );
549+
550+ const o1 = {
551+ b: [2 , 3 , 1 ],
552+ a: ' `a` comes before `b`' ,
553+ c: new Set ([2 , 3 , 1 ])
554+ };
555+ console .log (inspect (o1, { sorted: true }));
556+ // { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set { 1, 2, 3 } }
557+ console .log (inspect (o1, { sorted : (a , b ) => a < b }));
558+ // { c: Set { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }
559+
560+ const o2 = {
561+ c: new Set ([2 , 1 , 3 ]),
562+ a: ' `a` comes before `b`' ,
563+ b: [2 , 3 , 1 ]
564+ };
565+ assert .strict .equal (
566+ inspect (o1, { sorted: true }),
567+ inspect (o2, { sorted: true })
568+ );
569+ ```
570+
537571Please note that ` util.inspect() ` is a synchronous method that is mainly
538572intended as a debugging tool. Some input values can have a significant
539573performance overhead that can block the event loop. Use this function
@@ -2150,7 +2184,9 @@ Deprecated predecessor of `console.log`.
21502184[ WHATWG Encoding Standard ] : https://encoding.spec.whatwg.org/
21512185[ Common System Errors ] : errors.html#errors_common_system_errors
21522186[ async function ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
2187+ [ compare function ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Parameters
21532188[ constructor ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor
2189+ [ default sort ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
21542190[ global symbol registry ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for
21552191[ list of deprecated APIS ] : deprecations.html#deprecations_list_of_deprecated_apis
21562192[ semantically incompatible ] : https://github.com/nodejs/node/issues/4179
0 commit comments