import { hasOwn , TSError } from '@terascope/utils' ; import { ExpressionNode , isVariableNode , Options , VariableNode } from './interfaces' ; import { parseExpression } from './parse' ; returns export function evaluate ( input , options ) { const expr = typeof input === 'string' ? parseExpression ( input ) : input ; let output = '' ; for ( const node of expr . nodes ) { if ( isVariableNode ( node ) ) { output += evaluateVariableNode ( node , options ) ; } else { throw new Error ( `Unsupported expression "${ node . type }" given` ) ; } } return output ; } returns export function evaluateVariableNode ( node , { variables } ) { if ( hasOwn ( variables , node . value ) ) { return variables [ node . value ] ; } throw new TSError ( `Missing variable "${ node . value }" in expression` , { statusCode : 400 , context : { safe : true } } ) ; }