1- /* eslint-disable */
2-
31/**
42 * @see {@link https://github.com/douglascrockford/JSON-js/blob/master/cycle.js }
53 */
2321
2422/* jslint eval */
2523
26- export function decycle ( object , replacer ) {
24+ type ReplacerFunction = ( value : unknown ) => unknown ;
25+
26+ interface DecycledObject {
27+ [ key : string ] : unknown ;
28+ $ref ?: string ;
29+ }
30+
31+ export function decycle ( object : unknown , replacer ?: ReplacerFunction ) {
2732 'use strict' ;
2833
2934 // Make a deep copy of an object or array, assuring that there is at most
@@ -50,13 +55,13 @@ export function decycle(object, replacer) {
5055 // the object or array. [NUMBER] or [STRING] indicates a child element or
5156 // property.
5257
53- var objects = new window . WeakMap ( ) ; // object to path mappings
58+ const objects = new WeakMap < object , string > ( ) ; // object to path mappings
5459
55- return ( function derez ( value , path ) {
60+ return ( function derez ( value : unknown , path : string ) : unknown {
5661 // The derez function recurses through the object, producing the deep copy.
5762
58- var old_path ; // The path of an earlier occurance of value
59- var nu ; // The new object or array
63+ let old_path : string | undefined ; // The path of an earlier occurance of value
64+ let nu : unknown [ ] | DecycledObject ; // The new object or array
6065
6166 // If a replacer function was provided, then call it to get a replacement value.
6267
@@ -93,16 +98,18 @@ export function decycle(object, replacer) {
9398
9499 if ( Array . isArray ( value ) ) {
95100 nu = [ ] ;
96- value . forEach ( function ( element , i ) {
97- nu [ i ] = derez ( element , path + '[' + i + ']' ) ;
101+ ( value as unknown [ ] ) . forEach ( function ( element : unknown , i : number ) {
102+ ( nu as unknown [ ] ) [ i ] = derez ( element , path + '[' + String ( i ) + ']' ) ;
98103 } ) ;
99104 } else {
100105 // If it is an object, replicate the object.
101106
102- nu = { } ;
103- Object . keys ( value ) . forEach ( function ( name ) {
104- nu [ name ] = derez (
105- value [ name ] ,
107+ nu = { } as DecycledObject ;
108+ Object . keys ( value as Record < string , unknown > ) . forEach ( function (
109+ name : string ,
110+ ) {
111+ ( nu as DecycledObject ) [ name ] = derez (
112+ ( value as Record < string , unknown > ) [ name ] ,
106113 path + '[' + JSON . stringify ( name ) + ']' ,
107114 ) ;
108115 } ) ;
0 commit comments