ts-expression-evaluator
Context-based expression evaluator.
install
- npm
npm install ts-expression-evaluator
- yarn
yarn add ts-expression-evaluator
Quick start
import evaluate, { registerFunction } from 'ts-expression-evaluator'
const context = {
id: 10,
name: 'FED',
count: 10,
staffs: [{
id: 1,
name: 'Tina'
}, {
id: 2,
name: 'James'
}],
}
evaluate('1+2')
evaluate('name', context)
evaluate('staffs[0].name', context)
evaluate('count + 10', context)
evaluate('count === 11', context)
registerFunction('IF', (condition, ifTrue, ifFalse) => {
return condition ? ifTrue : ifFalse;
})
evaluate('IF(name === 'FED', 'It\'s FED.', 'It\'s not FED.')') // It's FED.
API
evaluate(exp: string, context: object): any
registerFunction(name: string, func: Function): void
registerFunctions(funcs: {[key: string]: Function}): void
License
MIT