import { TreeNode } from './tree-node' ; export class TreeFlattener { constructor ( public transformFunction ) { } _flattenNode ( node , level , resultNodes , parentMap ) { const flatNode = this . transformFunction ( node , level ) ; resultNodes . push ( flatNode ) ; if ( flatNode . expandable ) { const childrenNodes = node . children ; if ( Array . isArray ( childrenNodes ) ) { this . _flattenChildren ( childrenNodes , level , resultNodes , parentMap ) ; } else { this . _flattenChildren ( childrenNodes [ 0 ] , level , resultNodes , parentMap ) ; } } return resultNodes ; } _flattenChildren ( children , level , resultNodes , parentMap ) { children . forEach ( ( child , index ) => { const childParentMap = parentMap . slice ( ) ; childParentMap . push ( index !== children . length - 1 ) ; this . _flattenNode ( child , level + 1 , resultNodes , childParentMap ) ; } ) ; } flattenNodes ( structuredData ) { const resultNodes = [ ] ; structuredData . forEach ( node => this . _flattenNode ( node , 0 , resultNodes , [ ] ) ) ; return resultNodes ; } }