1
1
import { isPlainObject } from './_utils' ;
2
- import { encodePointer } from './encodePointer ' ;
2
+ import { pathToPointer } from './pathToPointer ' ;
3
3
4
4
export const decycle = ( obj : unknown , replacer ?: ( value : any ) => any ) => {
5
5
const objs = new WeakMap < object , string > ( ) ;
6
- return ( function derez ( value : any , path : string ) {
6
+ return ( function derez ( value : any , path : string [ ] ) {
7
7
// The new object or array
8
8
let curObj : any ;
9
9
@@ -15,22 +15,22 @@ export const decycle = (obj: unknown, replacer?: (value: any) => any) => {
15
15
const oldPath = objs . get ( value ) ;
16
16
// If the value is an object or array, look to see if we have already
17
17
// encountered it. If so, return a {"$ref":PATH} object.
18
- if ( oldPath ) return { $ref : encodePointer ( oldPath ) } ;
18
+ if ( oldPath ) return { $ref : oldPath } ;
19
19
20
- objs . set ( value , path ) ;
20
+ objs . set ( value , pathToPointer ( path ) ) ;
21
21
// If it is an array, replicate the array.
22
22
if ( Array . isArray ( value ) ) {
23
- curObj = value . map ( ( element , i ) => derez ( element , ` ${ path } / ${ i } ` ) ) ;
23
+ curObj = value . map ( ( element , i ) => derez ( element , [ ... path , String ( i ) ] ) ) ;
24
24
} else {
25
25
// It is an object, replicate the object.
26
26
curObj = { } ;
27
27
Object . keys ( value ) . forEach ( name => {
28
- curObj [ name ] = derez ( value [ name ] , ` ${ path } / ${ name } ` ) ;
28
+ curObj [ name ] = derez ( value [ name ] , [ ... path , name ] ) ;
29
29
} ) ;
30
30
}
31
31
objs . delete ( value ) ;
32
32
return curObj ;
33
33
}
34
34
return value ;
35
- } ) ( obj , '#' ) ;
35
+ } ) ( obj , [ ] ) ;
36
36
} ;
0 commit comments