import { DebugElement } from '@angular/core' ; import { MutableTree , Node , Path , tokenName , } from '../../tree' ; export const getNodeFromPartialPath = ( tree , path ) : Node => { const pindex = propertyIndex ( path ) ; return tree . traverse ( path . slice ( 0 , pindex ) ) ; } ; export const getPropertyPath = ( path ) : Path => { const index = propertyIndex ( path ) ; if ( index === path . length ) { return [ ] ; } return path . slice ( index ) ; } ; export const getInstanceFromPath = ( instance , path ) => { if ( instance == null ) { return null ; } const propertyPath = path . slice ( 0 ) ; while ( propertyPath . length > 0 ) { instance = instance [ propertyPath . shift ( ) ] ; if ( instance == null ) { return null ; } } return instance ; } ; export const getNodeProvider = ( element , providerToken , propertyPath ) => { const token = element . providerTokens . find ( t => tokenName ( t ) === providerToken ) ; if ( token == null ) { return null ; } const path = getPropertyPath ( propertyPath . slice ( 0 , propertyPath . length - 1 ) ) ; return getInstanceFromPath ( element . injector . get ( token ) , path ) ; } ; export const getNodeInstanceParent = ( element , path ) => { if ( path . length === 0 ) { return null ; } const propertyPath = getPropertyPath ( path . slice ( 0 , path . length - 1 ) ) ; if ( propertyPath . length > 0 ) { return getInstanceFromPath ( element . componentInstance , propertyPath ) ; } else { return element . componentInstance ; } } ; export const propertyIndex = ( path ) : number => { let index = 0 ; while ( index < path . length ) { if ( typeof path [ index ] !== 'number' ) { break ; } ++ index ; } return index ; } ;