| 240 | } |
| 241 | |
| 242 | class OperatorNode extends Node { |
| 243 | /** |
| 244 | * @constructor OperatorNode |
| 245 | * @extends {Node} |
| 246 | * An operator with two arguments, like 2+3 |
| 247 | * |
| 248 | * @param {string} op Operator name, for example '+' |
| 249 | * @param {string} fn Function name, for example 'add' |
| 250 | * @param {Node[]} args Operator arguments |
| 251 | * @param {boolean} [implicit] Is this an implicit multiplication? |
| 252 | * @param {boolean} [isPercentage] Is this an percentage Operation? |
| 253 | */ |
| 254 | constructor (op, fn, args, implicit, isPercentage) { |
| 255 | super() |
| 256 | // validate input |
| 257 | if (typeof op !== 'string') { |
| 258 | throw new TypeError('string expected for parameter "op"') |
| 259 | } |
| 260 | if (typeof fn !== 'string') { |
| 261 | throw new TypeError('string expected for parameter "fn"') |
| 262 | } |
| 263 | if (!Array.isArray(args) || !args.every(isNode)) { |
| 264 | throw new TypeError( |
| 265 | 'Array containing Nodes expected for parameter "args"') |
| 266 | } |
| 267 | |
| 268 | this.implicit = (implicit === true) |
| 269 | this.isPercentage = (isPercentage === true) |
| 270 | this.op = op |
| 271 | this.fn = fn |
| 272 | this.args = args || [] |
| 273 | } |
| 274 | |
| 275 | static name = name |
| 276 | get type () { return name } |
| 277 | get isOperatorNode () { return true } |
| 278 | |
| 279 | /** |
| 280 | * Compile a node into a JavaScript function. |
| 281 | * This basically pre-calculates as much as possible and only leaves open |
| 282 | * calculations which depend on a dynamic scope with variables. |
| 283 | * @param {Object} math Math.js namespace with functions and constants. |
| 284 | * @param {Object} argNames An object with argument names as key and `true` |
| 285 | * as value. Used in the SymbolNode to optimize |
| 286 | * for arguments from user assigned functions |
| 287 | * (see FunctionAssignmentNode) or special symbols |
| 288 | * like `end` (see IndexNode). |
| 289 | * @return {function} Returns a function which can be called like: |
| 290 | * evalNode(scope: Object, args: Object, context: *) |
| 291 | */ |
| 292 | _compile (math, argNames) { |
| 293 | // validate fn |
| 294 | if (typeof this.fn !== 'string' || !isSafeMethod(math, this.fn)) { |
| 295 | if (!math[this.fn]) { |
| 296 | throw new Error( |
| 297 | 'Function ' + this.fn + ' missing in provided namespace "math"') |
| 298 | } else { |
| 299 | throw new Error('No access to function "' + this.fn + '"') |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…