custom_evaluate_using_factories.js (715B)
1 const { create, evaluateDependencies, factory } = require('../..') 2 3 // custom implementations of all functions you want to support 4 const add = (a, b) => a + b 5 const subtract = (a, b) => a - b 6 const multiply = (a, b) => a * b 7 const divide = (a, b) => a / b 8 9 // create factories for the functions, and create an evaluate function with those 10 // these functions will also be used by the classes like Unit. 11 const { evaluate } = create({ 12 evaluateDependencies, 13 createAdd: factory('add', [], () => add), 14 createSubtract: factory('subtract', [], () => subtract), 15 createMultiply: factory('multiply', [], () => multiply), 16 createDivide: factory('divide', [], () => divide) 17 }) 18 19 console.log(evaluate('2 + 3 * 4')) // 14