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
52 lines (49 loc) • 1.57 kB
JavaScript
'use strict';
function factory(type, config, load, typed) {
var parse = load(require('../parse'));
/**
* Parse an expression. Returns a node tree, which can be evaluated by
* invoking node.eval().
*
* Note the evaluating arbitrary expressions may involve security risks,
* see [https://mathjs.org/docs/expressions/security.html](https://mathjs.org/docs/expressions/security.html) for more information.
*
* Syntax:
*
* math.parse(expr)
* math.parse(expr, options)
* math.parse([expr1, expr2, expr3, ...])
* math.parse([expr1, expr2, expr3, ...], options)
*
* Example:
*
* const node1 = math.parse('sqrt(3^2 + 4^2)')
* node1.compile().eval() // 5
*
* let scope = {a:3, b:4}
* const node2 = math.parse('a * b') // 12
* const code2 = node2.compile()
* code2.eval(scope) // 12
* scope.a = 5
* code2.eval(scope) // 20
*
* const nodes = math.parse(['a = 3', 'b = 4', 'a * b'])
* nodes[2].compile().eval() // 12
*
* See also:
*
* eval, compile
*
* @param {string | string[] | Matrix} expr Expression to be parsed
* @param {{nodes: Object<string, Node>}} [options] Available options:
* - `nodes` a set of custom nodes
* @return {Node | Node[]} node
* @throws {Error}
*/
return typed('parse', {
'string | Array | Matrix': parse,
'string | Array | Matrix, Object': parse
});
}
exports.name = 'parse';
exports.factory = factory;