UNPKG

2.22 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createEvaluate = void 0;
7
8var _collection = require("../../utils/collection");
9
10var _factory = require("../../utils/factory");
11
12var name = 'evaluate';
13var dependencies = ['typed', 'parse'];
14var createEvaluate =
15/* #__PURE__ */
16(0, _factory.factory)(name, dependencies, function (_ref) {
17 var typed = _ref.typed,
18 parse = _ref.parse;
19
20 /**
21 * Evaluate an expression.
22 *
23 * Note the evaluating arbitrary expressions may involve security risks,
24 * see [https://mathjs.org/docs/expressions/security.html](https://mathjs.org/docs/expressions/security.html) for more information.
25 *
26 * Syntax:
27 *
28 * math.evaluate(expr)
29 * math.evaluate(expr, scope)
30 * math.evaluate([expr1, expr2, expr3, ...])
31 * math.evaluate([expr1, expr2, expr3, ...], scope)
32 *
33 * Example:
34 *
35 * math.evaluate('(2+3)/4') // 1.25
36 * math.evaluate('sqrt(3^2 + 4^2)') // 5
37 * math.evaluate('sqrt(-4)') // 2i
38 * math.evaluate(['a=3', 'b=4', 'a*b']) // [3, 4, 12]
39 *
40 * let scope = {a:3, b:4}
41 * math.evaluate('a * b', scope) // 12
42 *
43 * See also:
44 *
45 * parse, compile
46 *
47 * @param {string | string[] | Matrix} expr The expression to be evaluated
48 * @param {Object} [scope] Scope to read/write variables
49 * @return {*} The result of the expression
50 * @throws {Error}
51 */
52 return typed(name, {
53 string: function string(expr) {
54 var scope = {};
55 return parse(expr).compile().evaluate(scope);
56 },
57 'string, Object': function stringObject(expr, scope) {
58 return parse(expr).compile().evaluate(scope);
59 },
60 'Array | Matrix': function ArrayMatrix(expr) {
61 var scope = {};
62 return (0, _collection.deepMap)(expr, function (entry) {
63 return parse(entry).compile().evaluate(scope);
64 });
65 },
66 'Array | Matrix, Object': function ArrayMatrixObject(expr, scope) {
67 return (0, _collection.deepMap)(expr, function (entry) {
68 return parse(entry).compile().evaluate(scope);
69 });
70 }
71 });
72});
73exports.createEvaluate = createEvaluate;
\No newline at end of file