@@ -360,6 +360,9 @@ stream.write('With ES6');
360
360
<!-- YAML
361
361
added: v0.3.0
362
362
changes:
363
+ - version: REPLACEME
364
+ pr-url: https://github.com/nodejs/node/pull/22788
365
+ description: The `sorted` option is supported now.
363
366
- version: v10.6.0
364
367
pr-url: https://github.com/nodejs/node/pull/20725
365
368
description: Inspecting linked lists and similar objects is now possible
@@ -420,7 +423,10 @@ changes:
420
423
objects the same as arrays. Note that no text will be reduced below 16
421
424
characters, no matter the ` breakLength ` size. For more information, see the
422
425
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] [ ] .
424
430
* Returns: {string} The representation of passed object
425
431
426
432
The ` util.inspect() ` method returns a string representation of ` object ` that is
@@ -534,6 +540,34 @@ console.log(inspect(weakSet, { showHidden: true }));
534
540
// WeakSet { { a: 1 }, { b: 2 } }
535
541
```
536
542
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
+
537
571
Please note that ` util.inspect() ` is a synchronous method that is mainly
538
572
intended as a debugging tool. Some input values can have a significant
539
573
performance overhead that can block the event loop. Use this function
@@ -2150,7 +2184,9 @@ Deprecated predecessor of `console.log`.
2150
2184
[ WHATWG Encoding Standard ] : https://encoding.spec.whatwg.org/
2151
2185
[ Common System Errors ] : errors.html#errors_common_system_errors
2152
2186
[ 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
2153
2188
[ 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
2154
2190
[ global symbol registry ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for
2155
2191
[ list of deprecated APIS ] : deprecations.html#deprecations_list_of_deprecated_apis
2156
2192
[ semantically incompatible ] : https://github.com/nodejs/node/issues/4179
0 commit comments