| 23 | } |
| 24 | |
| 25 | class Node { |
| 26 | get type () { return 'Node' } |
| 27 | get isNode () { return true } |
| 28 | |
| 29 | /** |
| 30 | * Evaluate the node |
| 31 | * @param {Object} [scope] Scope to read/write variables |
| 32 | * @return {*} Returns the result |
| 33 | */ |
| 34 | evaluate (scope) { |
| 35 | return this.compile().evaluate(scope) |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Compile the node into an optimized, evauatable JavaScript function |
| 40 | * @return {{evaluate: function([Object])}} object |
| 41 | * Returns an object with a function 'evaluate', |
| 42 | * which can be invoked as expr.evaluate([scope: Object]), |
| 43 | * where scope is an optional object with |
| 44 | * variables. |
| 45 | */ |
| 46 | compile () { |
| 47 | const expr = this._compile(mathWithTransform, {}) |
| 48 | const args = {} |
| 49 | const context = null |
| 50 | |
| 51 | function evaluate (scope) { |
| 52 | const s = createMap(scope) |
| 53 | _validateScope(s) |
| 54 | return expr(s, args, context) |
| 55 | } |
| 56 | |
| 57 | return { |
| 58 | evaluate |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Compile a node into a JavaScript function. |
| 64 | * This basically pre-calculates as much as possible and only leaves open |
| 65 | * calculations which depend on a dynamic scope with variables. |
| 66 | * @param {Object} math Math.js namespace with functions and constants. |
| 67 | * @param {Object} argNames An object with argument names as key and `true` |
| 68 | * as value. Used in the SymbolNode to optimize |
| 69 | * for arguments from user assigned functions |
| 70 | * (see FunctionAssignmentNode) or special symbols |
| 71 | * like `end` (see IndexNode). |
| 72 | * @return {function} Returns a function which can be called like: |
| 73 | * evalNode(scope: Object, args: Object, context: *) |
| 74 | */ |
| 75 | _compile (math, argNames) { |
| 76 | throw new Error('Method _compile must be implemented by type ' + this.type) |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Execute a callback for each of the child nodes of this node |
| 81 | * @param {function(child: Node, path: string, parent: Node)} callback |
| 82 | */ |
no outgoing calls
no test coverage detected
searching dependent graphs…