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 {
5656 if ( typeof obj !== "object" || ! obj ) {
5757 return obj ;
5858 }
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 ) {
6161 return obj . toJSON ( ) ;
6262 }
63- // Only check for circularity among ancestors in the recursion stack.
6463 if ( refs . includes ( obj ) ) {
6564 return "[Circular]" ;
65+ } else {
66+ refs . push ( obj ) ;
6667 }
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.
7168 let returnObj : any ;
7269 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 {
7772 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 ) ;
8079 }
8180 }
82-
83- // Remove the current object from the stack once its properties are processed.
8481 refs . pop ( ) ;
8582 return returnObj ;
8683}
You can’t perform that action at this time.
0 commit comments