file author tony7lee
  import { ICollectItem } from '../types/collector' ; param { * } target param { * } collect returns function nestLoop ( target , collect ) { const { routePath } = target ; const out = [ ] ; const targetChildren = [ ] ; let targetHandled = false ; collect . forEach ( i => { if ( routePath === i . routePath ) { targetHandled = true ; out . push ( { ... i , ... target } ) ; } else if ( routePath . indexOf ( i . routePath ) === 0 ) { targetHandled = true ; if ( i . children && i . children . filter ( j => ( routePath . indexOf ( j . routePath ) === 0 ) ) . length ) { i . children = nestLoop ( target , i . children ) ; } else { if ( ! i . children ) i . children = [ ] ; i . children . push ( target ) ; } out . push ( i ) ; } else if ( i . routePath . indexOf ( routePath ) === 0 ) { targetHandled = true ; targetChildren . push ( i ) ; } else { out . push ( i ) ; } } ) ; if ( targetChildren . length ) target . children = targetChildren ; if ( ! targetHandled ) { out . push ( target ) ; } return out ; } param { string } closestLayoutPath param { * } target param { * } collect function pushClosestNode ( closestLayoutPath , target , collect ) { collect . forEach ( i => { if ( i . routePath === closestLayoutPath ) { if ( ! i . children ) i . children = [ ] ; i . children . push ( target ) ; } else if ( i . children && i . children . length ) { pushClosestNode ( closestLayoutPath , target , i . children ) ; } } ) ; } export default function nest ( layoutMap , pageConfig , routesCollect ) { let out ; const layoutKeys = Object . keys ( layoutMap ) ; const { routePath , isLayoutLike } = pageConfig ; if ( isLayoutLike ) { out = nestLoop ( pageConfig , routesCollect ) ; } else { let closer = 0 ; let closestLayoutPath ; layoutKeys . forEach ( layoutPath => { const layoutSectionCount = layoutPath . split ( '/' ) . length ; if ( routePath . indexOf ( layoutPath ) === 0 && layoutSectionCount > closer ) { closer = layoutSectionCount ; closestLayoutPath = layoutPath ; } } ) ; if ( closestLayoutPath ) { pushClosestNode ( closestLayoutPath , pageConfig , routesCollect ) ; } else { routesCollect . push ( pageConfig ) ; } out = routesCollect ; } return out ; }