File tree Expand file tree Collapse file tree 1 file changed +12
-15
lines changed Expand file tree Collapse file tree 1 file changed +12
-15
lines changed Original file line number Diff line number Diff line change @@ -56,31 +56,28 @@ function removeCircular(obj: any, refs: any[] = []): any {
56
56
if ( typeof obj !== "object" || ! obj ) {
57
57
return obj ;
58
58
}
59
- // If the object defines its own toJSON method, use it .
60
- if ( obj . toJSON && typeof obj . toJSON === "function" ) {
59
+ // If the object defines its own toJSON, prefer that .
60
+ if ( obj . toJSON ) {
61
61
return obj . toJSON ( ) ;
62
62
}
63
- // Only check for circularity among ancestors in the recursion stack.
64
63
if ( refs . includes ( obj ) ) {
65
64
return "[Circular]" ;
65
+ } else {
66
+ refs . push ( obj ) ;
66
67
}
67
- // Add the current object to the recursion stack.
68
- refs . push ( obj ) ;
69
-
70
- // If the object is an array, run circular check on each element.
71
68
let returnObj : any ;
72
69
if ( Array . isArray ( obj ) ) {
73
- returnObj = obj . map ( ( item ) => removeCircular ( item , refs ) ) ;
74
- }
75
- // If the object is a Map, run circular check on each key and value.
76
- else {
70
+ returnObj = new Array ( obj . length ) ;
71
+ } else {
77
72
returnObj = { } ;
78
- for ( const key of Object . keys ( obj ) ) {
79
- returnObj [ key ] = removeCircular ( obj [ key ] , refs ) ;
73
+ }
74
+ for ( const k in obj ) {
75
+ if ( refs . includes ( obj [ k ] ) ) {
76
+ returnObj [ k ] = "[Circular]" ;
77
+ } else {
78
+ returnObj [ k ] = removeCircular ( obj [ k ] , refs ) ;
80
79
}
81
80
}
82
-
83
- // Remove the current object from the stack once its properties are processed.
84
81
refs . pop ( ) ;
85
82
return returnObj ;
86
83
}
You can’t perform that action at this time.
0 commit comments