@@ -47,10 +47,10 @@ export function deepFreeze<T>(obj: T): T {
4747const _hasOwnProperty = Object . prototype . hasOwnProperty ;
4848
4949export function cloneAndChange ( obj : any , changer : ( orig : any ) => any ) : any {
50- return _cloneAndChange ( obj , changer , [ ] ) ;
50+ return _cloneAndChange ( obj , changer , new Set ( ) ) ;
5151}
5252
53- function _cloneAndChange ( obj : any , changer : ( orig : any ) => any , encounteredObjects : any [ ] ) : any {
53+ function _cloneAndChange ( obj : any , changer : ( orig : any ) => any , seen : Set < any > ) : any {
5454 if ( isUndefinedOrNull ( obj ) ) {
5555 return obj ;
5656 }
@@ -63,23 +63,23 @@ function _cloneAndChange(obj: any, changer: (orig: any) => any, encounteredObjec
6363 if ( isArray ( obj ) ) {
6464 const r1 : any [ ] = [ ] ;
6565 for ( let i1 = 0 ; i1 < obj . length ; i1 ++ ) {
66- r1 . push ( _cloneAndChange ( obj [ i1 ] , changer , encounteredObjects ) ) ;
66+ r1 . push ( _cloneAndChange ( obj [ i1 ] , changer , seen ) ) ;
6767 }
6868 return r1 ;
6969 }
7070
7171 if ( isObject ( obj ) ) {
72- if ( encounteredObjects . indexOf ( obj ) >= 0 ) {
72+ if ( seen . has ( obj ) ) {
7373 throw new Error ( 'Cannot clone recursive data-structure' ) ;
7474 }
75- encounteredObjects . push ( obj ) ;
75+ seen . add ( obj ) ;
7676 const r2 = { } ;
7777 for ( let i2 in obj ) {
7878 if ( _hasOwnProperty . call ( obj , i2 ) ) {
79- ( r2 as any ) [ i2 ] = _cloneAndChange ( obj [ i2 ] , changer , encounteredObjects ) ;
79+ ( r2 as any ) [ i2 ] = _cloneAndChange ( obj [ i2 ] , changer , seen ) ;
8080 }
8181 }
82- encounteredObjects . pop ( ) ;
82+ seen . delete ( obj ) ;
8383 return r2 ;
8484 }
8585
0 commit comments