UNPKG

mathjs

Version:

Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif

35 lines (31 loc) 1.07 kB
'use strict' function factory (type, config, load, typed) { /** * Compile an inline expression like "x > 0" * @param {Node} expression * @param {Object} math * @param {Object} scope * @return {function} Returns a function with one argument which fills in the * undefined variable (like "x") and evaluates the expression */ return function compileInlineExpression (expression, math, scope) { // find an undefined symbol const symbol = expression.filter(function (node) { return type.isSymbolNode(node) && !(node.name in math) && !(node.name in scope) })[0] if (!symbol) { throw new Error('No undefined variable found in inline expression "' + expression + '"') } // create a test function for this equation const name = symbol.name // variable name const subScope = Object.create(scope) const eq = expression.compile() return function inlineExpression (x) { subScope[name] = x return eq.eval(subScope) } } } exports.factory = factory