import chalk from 'chalk' ; import { DecisionTree , FlattenedApiList , FlattenedApiNode , TreeNode } from './interfaces' ; export param { Tree } tree param { FlattenedApiList } apiList returns { DecisionTree } export function decisionTreeReducer ( tree , apiList ) { return tree . reduce ( ( acc , curr ) => { let nested ; let treeNode = { id : curr . id , label : curr . label } ; if ( curr . options ) { treeNode = { ... treeNode , options : curr . options } ; } if ( ! curr . options ) { const apiNode = apiList [ treeNode . label ] ; if ( ! apiNode ) { console . log ( chalk . yellow ( 'Decision Tree Generator - (reducer) - warning:' ) , `Label does not exist in API List: ${ treeNode . label }` ) ; } treeNode = { ... treeNode , ... apiNode } ; } if ( curr . method ) { treeNode = { ... treeNode , method : curr . method } ; } if ( curr . children ) { nested = decisionTreeReducer ( curr . children , apiList ) ; } return { ... acc , ... nested , [ treeNode . id ] : treeNode } ; } , { } ) ; }