1

I'm using mathjs for calculating some expressions from JSON data. I call eval, where I pass JSON data as a scope. I can use JSON paths in the mathjs script, like user1.age > user2.age. But when these paths are not contained in JSON, mathjs throws an error.

If the symbol is null, I can easily check it in the mathjs script. But if it is undefined, I cannot do it, because mathjs throws error, instead of evaluating it to undefined.

left == null ? right : left

This script evaluates to 42 for the following input -

{
  "left": null,
  "right": 42
}

But throws an error "Error: Undefined symbol left" for this -

{
  "right": 42
}

Tried to traverse expression tree, and add all of the missing symbols to the JSON data, and pass it to the eval later. https://mathjs.org/examples/advanced/expression_trees.js.html But is there a better way for custom symbols handling?

Rob Yart
  • 323
  • 1
  • 2
  • 13

0 Answers0