import { NodePathSegmentFormatter } from "../types" ; import { walk } from "../walk" ; function updateObjectViaPathString ( obj , path , delimiter , val ) { const block = path . split ( delimiter ) . slice ( 1 ) ; while ( block . length > 1 ) obj = obj [ block . shift ( ) ! ] ; obj [ block . shift ( ) ! ] = val ; } export function deepCopy ( target , delimiter = '$walk:dc$' ) { if ( target === null ) return null ; const newObj = Array . isArray ( target ) ? [ ] : { } ; const format = ( { key } ) => delimiter + key ; walk ( target , { rootObjectCallbacks : false , callbacks : [ { positionFilter : 'preWalk' , callback : ( node ) => updateObjectViaPathString ( newObj , node . getPath ( format ) , delimiter , node . nodeType === 'array' ? [ ] : ( node . nodeType === 'value' ? node . val : ( node . val === null ? null : { } ) ) ) } ] } ) ; return newObj ; }