custom_evaluate_using_import.js (676B)
1 const { create, evaluateDependencies } = 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 a mathjs instance with hardly any functions 10 // there are some functions created which are used internally by evaluate though, 11 // for example by the Unit class which has dependencies on addScalar, subtract, 12 // multiplyScalar, etc. 13 const math = create(evaluateDependencies) 14 15 // import your own functions 16 math.import({ add, subtract, multiply, divide }, { override: true }) 17 18 console.log(math.evaluate('2 + 3 * 4')) // 14