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