1
- var has = Object . hasOwnProperty
2
1
exports = module . exports = stringify
3
2
exports . getSerialize = serializer
4
3
@@ -7,35 +6,26 @@ function stringify(obj, replacer, spaces, cycleReplacer) {
7
6
}
8
7
9
8
function serializer ( replacer , cycleReplacer ) {
10
- var stack = [ ]
9
+ var stack = [ ] , keys = [ ]
11
10
12
11
if ( cycleReplacer == null ) cycleReplacer = function ( key , value ) {
13
- return pathize ( stack , key , value )
12
+ return pathize ( stack , keys , value )
14
13
}
15
14
16
15
return function ( key , value ) {
17
16
if ( stack . length > 0 ) {
18
17
var thisPos = stack . indexOf ( this )
19
18
~ thisPos ? stack . splice ( thisPos + 1 ) : stack . push ( this )
19
+ ~ thisPos ? keys . splice ( thisPos , Infinity , key ) : keys . push ( key )
20
20
if ( ~ stack . indexOf ( value ) ) value = cycleReplacer . call ( this , key , value )
21
21
}
22
22
else stack . push ( value )
23
23
24
- return replacer ? replacer . call ( this , key , value ) : value
24
+ return replacer == null ? value : replacer . call ( this , key , value )
25
25
}
26
26
}
27
27
28
- function pathize ( stack , key , value ) {
29
- var paths = [ "" ]
30
-
31
- for ( var i = 0 , l = stack . indexOf ( value ) ; i < l ; ++ i )
32
- paths . push ( findKey ( stack [ i ] , stack [ i + 1 ] ) )
33
-
34
- return "[Circular ~" + paths . join ( "." ) + "]"
35
- }
36
-
37
- function findKey ( obj , value ) {
38
- // For arrays from foreign context, for-in will probably do.
39
- if ( obj instanceof Array ) return obj . indexOf ( value )
40
- for ( var key in obj ) if ( has . call ( obj , key ) && obj [ key ] === value ) return key
28
+ function pathize ( stack , keys , value ) {
29
+ if ( stack [ 0 ] === value ) return "[Circular ~]"
30
+ return "[Circular ~." + keys . slice ( 0 , stack . indexOf ( value ) ) . join ( "." ) + "]"
41
31
}
0 commit comments