import { TBaseObj } from '../../typings' ; import { isObj } from './is-obj' ; const traversal = < T extends TBaseObj > ( condition , cb , obj , path , visited , deep ) => { if ( visited . has ( obj ) ) { return ; } visited . add ( obj ) ; if ( ! deep ) { if ( condition ( obj ) ) { cb ( obj as T , path ) ; return ; } } path . push ( obj ) ; if ( Array . isArray ( obj ) ) { for ( let i = 0 ; i < obj . length ; ) { const item = obj [ i ] ; traversal ( condition , cb , item , path , visited , deep ) ; if ( item === obj [ i ] ) { i ++ ; } } } else { for ( const key in obj ) { const objK = obj [ key ] ; if ( isObj < T > ( objK ) ) { traversal ( condition , cb , objK , path , visited , deep ) ; } } } path . pop ( ) ; if ( deep ) { if ( condition ( obj ) ) { cb ( obj as T , path ) ; } } } ; export const traversalObj = < T extends TBaseObj > ( condition , cb , obj , deep = false ) => { traversal < T > ( condition , cb , obj , [ ] , new Set < T | T [ ] > ( ) , deep ) ; } ;