@@ -5,45 +5,47 @@ import { serializeObject } from './object';
55import { stableSerializeArray } from './array-stable' ;
66import { stableSerializeObject } from './object-stable' ;
77
8- type CompareFunction = ( a : [ string , any ] , b : [ string , any ] ) => number ;
8+ type CompareFunction = ( a : string , b : string ) => number ;
99
1010/**
11- * jsonStringify() converts the provided object to a JSON string and returns it. If true is passed as the second
12- * argument then nested objects will be checked for circular references and an error will be thrown if one is found.
13- * If false is passed then there will be no checks for circular references, which grants a considerable speed boost .
11+ * Converts the provided object to a JSON string and returns it. If true is passed as the second argument then nested
12+ * objects will be checked for circular references and an error will be thrown if one is found. If false is passed then
13+ * there will be no checks for circular references.
1414 */
1515
1616export function jsonStringify ( value : any , safe = true ) : string | undefined {
17- if ( typeof value === 'boolean' ) {
18- return value . toString ( ) ;
19- }
17+ if ( typeof value === 'object' ) {
18+ if ( value === null ) {
19+ return 'null' ;
20+ }
2021
21- else if ( typeof value === 'number' ) {
22- return ( value === Infinity || Number . isNaN ( value ) ? 'null' : value . toString ( ) ) ;
22+ if ( value instanceof Date ) {
23+ return '"' + value . toISOString ( ) + '"' ;
24+ }
25+
26+ if ( Buffer . isBuffer ( value ) ) {
27+ return serializeBuffer ( value ) ;
28+ }
29+
30+ if ( Array . isArray ( value ) ) {
31+ return serializeArray ( value , safe ) ;
32+ }
33+
34+ return serializeObject ( value , safe ) ;
2335 }
2436
2537 else if ( typeof value === 'string' ) {
2638 return serializeString ( value ) ;
2739 }
2840
29- else if ( value === null ) {
30- return 'null' ;
41+ else if ( typeof value === 'number' ) {
42+ return ( value === Infinity || Number . isNaN ( value ) )
43+ ? 'null'
44+ : value . toString ( ) ;
3145 }
3246
33- else if ( typeof value === 'object' ) {
34- if ( Buffer . isBuffer ( value ) ) {
35- return serializeBuffer ( value ) ;
36- }
37-
38- else if ( value instanceof Date ) {
39- return '"' + value . toISOString ( ) + '"' ;
40- }
41-
42- else {
43- return Array . isArray ( value )
44- ? serializeArray ( value , safe )
45- : serializeObject ( value , safe ) ;
46- }
47+ else if ( typeof value === 'boolean' ) {
48+ return value . toString ( ) ;
4749 }
4850
4951 else if ( typeof value === 'bigint' ) {
@@ -54,44 +56,46 @@ export function jsonStringify(value: any, safe = true): string | undefined {
5456}
5557
5658/**
57- * stableJsonStringify() is a deterministic version of jsonStringify(). It works the same with the exception that object
58- * properties are sorted before being serialized, resulting in consistent output for the same input. By default object
59- * keys are sorted alphabetically, but a custom compare function can be passed to control the sorting behavior.
59+ * A deterministic version of jsonStringify(). It works the same with the exception that object properties are sorted
60+ * before being serialized, resulting in consistent output for the same input. By default object keys are sorted
61+ * alphabetically, but a custom compare function can be passed to control the sorting behavior.
6062 */
6163
6264export function stableJsonStringify ( value : any , compareFn ?: CompareFunction | null , safe = true ) : string | undefined {
63- if ( typeof value === 'boolean' ) {
64- return value . toString ( ) ;
65- }
66-
67- else if ( typeof value === 'number' ) {
68- return ( value === Infinity || Number . isNaN ( value ) ? 'null' : value . toString ( ) ) ;
69- }
70-
71- else if ( typeof value === 'string' ) {
72- return serializeString ( value ) ;
73- }
65+ if ( typeof value === 'object' ) {
66+ if ( value === null ) {
67+ return 'null' ;
68+ }
7469
75- else if ( value === null ) {
76- return 'null ' ;
77- }
70+ if ( value instanceof Date ) {
71+ return '"' + value . toISOString ( ) + '" ';
72+ }
7873
79- else if ( typeof value === 'object' ) {
8074 if ( Buffer . isBuffer ( value ) ) {
8175 return serializeBuffer ( value ) ;
8276 }
8377
84- else if ( value instanceof Date ) {
85- return '"' + value . toISOString ( ) + '"' ;
78+ compareFn = compareFn ?? defaultCompareKeys ;
79+
80+ if ( Array . isArray ( value ) ) {
81+ return stableSerializeArray ( value , compareFn , safe ) ;
8682 }
8783
88- else {
89- compareFn = typeof compareFn === 'function' ? compareFn : defaultCompareKeys ;
84+ return stableSerializeObject ( value , compareFn , safe ) ;
85+ }
9086
91- return Array . isArray ( value )
92- ? stableSerializeArray ( value , compareFn , safe )
93- : stableSerializeObject ( value , compareFn , safe ) ;
94- }
87+ else if ( typeof value === 'string' ) {
88+ return serializeString ( value ) ;
89+ }
90+
91+ else if ( typeof value === 'number' ) {
92+ return ( value === Infinity || Number . isNaN ( value ) )
93+ ? 'null'
94+ : value . toString ( ) ;
95+ }
96+
97+ else if ( typeof value === 'boolean' ) {
98+ return value . toString ( ) ;
9599 }
96100
97101 else if ( typeof value === 'bigint' ) {
@@ -101,6 +105,6 @@ export function stableJsonStringify(value: any, compareFn?: CompareFunction | nu
101105 return ;
102106}
103107
104- function defaultCompareKeys ( [ keyA ] : string [ ] , [ keyB ] : string [ ] ) {
105- return keyA > keyB ? 1 : - 1 ;
108+ function defaultCompareKeys ( a : string , b : string ) {
109+ return a > b ? 1 : - 1 ;
106110}
0 commit comments