1
+ 'use strict' ;
2
+
3
+ /*
4
+ json-stringify-safe
5
+ Like JSON.stringify, but doesn't throw on circular references.
6
+
7
+ Originally forked from https://github.com/isaacs/json-stringify-safe
8
+ version 5.0.1 on 3/8/2017 and modified for IE8 compatibility.
9
+ Tests for this are in test/vendor.
10
+
11
+ ISC license: https://github.com/isaacs/json-stringify-safe/blob/master/LICENSE
12
+ */
13
+
1
14
exports = module . exports = stringify
2
15
exports . getSerialize = serializer
3
16
17
+ function indexOf ( haystack , needle ) {
18
+ for ( var i = 0 ; i < haystack . length ; ++ i ) {
19
+ if ( haystack [ i ] === needle ) return i ;
20
+ }
21
+ return - 1 ;
22
+ }
23
+
4
24
function stringify ( obj , replacer , spaces , cycleReplacer ) {
5
25
return JSON . stringify ( obj , serializer ( replacer , cycleReplacer ) , spaces )
6
26
}
@@ -9,19 +29,19 @@ function serializer(replacer, cycleReplacer) {
9
29
var stack = [ ] , keys = [ ]
10
30
11
31
if ( cycleReplacer == null ) cycleReplacer = function ( key , value ) {
12
- if ( stack [ 0 ] === value ) return " [Circular ~]"
13
- return " [Circular ~." + keys . slice ( 0 , stack . indexOf ( value ) ) . join ( "." ) + "]"
32
+ if ( stack [ 0 ] === value ) return ' [Circular ~]'
33
+ return ' [Circular ~.' + keys . slice ( 0 , indexOf ( stack , value ) ) . join ( '.' ) + ']'
14
34
}
15
35
16
36
return function ( key , value ) {
17
37
if ( stack . length > 0 ) {
18
- var thisPos = stack . indexOf ( this )
38
+ var thisPos = indexOf ( stack , this ) ;
19
39
~ thisPos ? stack . splice ( thisPos + 1 ) : stack . push ( this )
20
40
~ thisPos ? keys . splice ( thisPos , Infinity , key ) : keys . push ( key )
21
- if ( ~ stack . indexOf ( value ) ) value = cycleReplacer . call ( this , key , value )
41
+ if ( ~ indexOf ( stack , value ) ) value = cycleReplacer . call ( this , key , value )
22
42
}
23
43
else stack . push ( value )
24
44
25
45
return replacer == null ? value : replacer . call ( this , key , value )
26
46
}
27
- }
47
+ }
0 commit comments