File tree Expand file tree Collapse file tree 1 file changed +9
-5
lines changed Expand file tree Collapse file tree 1 file changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,9 @@ function stringifyDeterministic (obj, opts = DEFAULTS) {
28
28
}
29
29
} ) ( opts . compare )
30
30
31
+ // Detect circular structure in obj and raise error efficiently.
32
+ if ( ! cycles ) stringify ( obj )
33
+
31
34
const seen = [ ]
32
35
33
36
return ( function _deterministic ( parent , key , node , level ) {
@@ -49,11 +52,12 @@ function stringifyDeterministic (obj, opts = DEFAULTS) {
49
52
}
50
53
return '[' + out . join ( ',' ) + indent + ']'
51
54
} else {
52
- if ( seen . indexOf ( node ) !== - 1 ) {
53
- if ( cycles ) return stringify ( '[Circular]' )
54
- throw new TypeError ( 'Converting circular structure to JSON' )
55
- } else {
56
- seen . push ( node )
55
+ if ( cycles ) {
56
+ if ( seen . indexOf ( node ) !== - 1 ) {
57
+ return stringify ( '[Circular]' )
58
+ } else {
59
+ seen . push ( node )
60
+ }
57
61
}
58
62
59
63
const nodeKeys = keys ( node ) . sort ( compare && compare ( node ) )
You can’t perform that action at this time.
0 commit comments