import { MutableTree , Node , Path , deserializePath , serializePath , } from '../../tree' ; import { Dependency } from '../../backend/utils/description' ; export class ParseUtils { getParentNodeIds ( nodeId ) { const path = deserializePath ( nodeId ) ; const result = new Array < string > ( ) ; for ( let i = 1 ; i < path . length ; ++ i ) { result . push ( serializePath ( path . slice ( 0 , i ) ) ) ; } return result ; } getNodeDependency ( node , dependencyId ) { return node . dependencies . reduce ( ( prev , curr , idx , p ) => prev ? prev : p [ idx ] . id === dependencyId ? p [ idx ] : null , null ) ; } checkNodeProvidesDependency ( node , dependency ) { return node . providers . reduce ( ( prev , curr , idx , p ) => prev ? prev : p [ idx ] . id === dependency . id , false ) ; } getDependencyProvider ( tree , nodeId , dependency ) { if ( tree == null ) { return null ; } const node = tree . lookup ( nodeId ) ; if ( this . checkNodeProvidesDependency ( node , dependency ) && dependency . decorators . indexOf ( '@SkipSelf' ) < 0 ) { return node ; } const nodeIds = this . getParentNodeIds ( nodeId ) ; for ( const id of nodeIds ) { const matchingNode = tree . lookup ( id ) ; if ( this . checkNodeProvidesDependency ( matchingNode , dependency ) ) { return matchingNode ; } } return null ; } getParentHierarchy ( tree , node , filter ? ) { if ( tree == null ) { return [ ] ; } const nodeIds = this . getParentNodeIds ( node . id ) ; const hierarchy = nodeIds . reduce ( ( array , id ) => { const matchingNode = tree . lookup ( id ) ; if ( matchingNode ) { array . push ( matchingNode ) ; } return array ; } , [ ] ) ; if ( typeof filter === 'function' ) { return hierarchy . filter ( n => filter ( n ) ) ; } return hierarchy ; } flatten ( list ) { return list . reduce ( ( a , b ) => a . concat ( Array . isArray ( b . children ) ? [ this . copyParent ( b ) , ... this . flatten ( b . children ) ] : b ) , [ ] ) ; } copyParent ( p ) { return Object . assign ( { } , p , { children : undefined } ) ; } }