@@ -54,9 +54,9 @@ interface DecycledObject {
5454 * @returns A deep copy of the object with circular references replaced by `$ref` objects.
5555 */
5656export function decycle ( object : unknown , replacer ?: ReplacerFunction ) {
57- const objects = new WeakMap < object , string > ( ) ; // object to path mappings
57+ const visitedObjects = new WeakMap < object , string > ( ) ; // object to path mappings
5858
59- return deepCopy ( object , '$' , objects , replacer ) ;
59+ return deepCopy ( object , '$' , visitedObjects , replacer ) ;
6060}
6161
6262/**
@@ -65,18 +65,18 @@ export function decycle(object: unknown, replacer?: ReplacerFunction) {
6565 *
6666 * @param value - The current value to copy.
6767 * @param path - The JSONPath to the current value.
68- * @param objects - WeakMap tracking already-visited objects and their paths.
68+ * @param visitedObjects - WeakMap tracking already-visited objects and their paths.
6969 * @param replacer - Optional replacer function called for each value.
7070 * @returns The deep-copied value.
7171 */
7272function deepCopy (
7373 value : unknown ,
7474 path : string ,
75- objects : WeakMap < object , string > ,
75+ visitedObjects : WeakMap < object , string > ,
7676 replacer ?: ReplacerFunction ,
7777) : unknown {
78- let old_path : string | undefined ;
79- let nu : unknown [ ] | DecycledObject ;
78+ let existingPath : string | undefined ;
79+ let copy : unknown [ ] | DecycledObject ;
8080
8181 if ( replacer !== undefined ) {
8282 value = replacer ( value ) ;
@@ -91,37 +91,37 @@ function deepCopy(
9191 ! ( value instanceof RegExp ) &&
9292 ! ( value instanceof String )
9393 ) {
94- old_path = objects . get ( value ) ;
95- if ( old_path !== undefined ) {
96- return { $ref : old_path } ;
94+ existingPath = visitedObjects . get ( value ) ;
95+ if ( existingPath !== undefined ) {
96+ return { $ref : existingPath } ;
9797 }
9898
99- objects . set ( value , path ) ;
99+ visitedObjects . set ( value , path ) ;
100100
101101 if ( Array . isArray ( value ) ) {
102- nu = [ ] ;
103- ( value as unknown [ ] ) . forEach ( function ( element : unknown , i : number ) {
104- ( nu as unknown [ ] ) [ i ] = deepCopy (
102+ copy = [ ] ;
103+ ( value as unknown [ ] ) . forEach ( function ( element : unknown , index : number ) {
104+ ( copy as unknown [ ] ) [ index ] = deepCopy (
105105 element ,
106- path + '[' + String ( i ) + ']' ,
107- objects ,
106+ path + '[' + String ( index ) + ']' ,
107+ visitedObjects ,
108108 replacer ,
109109 ) ;
110110 } ) ;
111111 } else {
112- nu = { } as DecycledObject ;
112+ copy = { } as DecycledObject ;
113113 Object . keys ( value as Record < string , unknown > ) . forEach ( function (
114- name : string ,
114+ key : string ,
115115 ) {
116- ( nu as DecycledObject ) [ name ] = deepCopy (
117- ( value as Record < string , unknown > ) [ name ] ,
118- path + '[' + JSON . stringify ( name ) + ']' ,
119- objects ,
116+ ( copy as DecycledObject ) [ key ] = deepCopy (
117+ ( value as Record < string , unknown > ) [ key ] ,
118+ path + '[' + JSON . stringify ( key ) + ']' ,
119+ visitedObjects ,
120120 replacer ,
121121 ) ;
122122 } ) ;
123123 }
124- return nu ;
124+ return copy ;
125125 }
126126 return value ;
127127}
0 commit comments