Ability to override operators such as the property accessor #3054
Replies: 2 comments
-
Thanks for your suggestions @krashdifferent. The property getters and setters of mathjs are currently hardcoded and cannot be customized. To make the expression parser secure, this getter and setter have special security rules built in (for example to prevent you from getting access to Just thinking aloud:
|
Beta Was this translation helpful? Give feedback.
-
This seems like more of an open-ended proposal. Moving to Discussions. |
Beta Was this translation helpful? Give feedback.
-
I am looking for a way to override operators in the expression evaluator, particularly the property accessor. Essentially I would like to apply an optional transform function to this operator but it is unclear to me if this is possible.
Specific use cases:
While I agree with the default behavior, there a couple example extensions I am looking to add that better fits our workflow. In this case users are writing a short script that gets run on a lot of object data entries.
Deeply nested objects are common in our data, however it is also common for an early value in the chain to be undefined or null. I would like to make attempting to access a property on undefined or null return undefined or null rather than throw an error. The expression
a.b.c.d
would return undefined ifa.b
is undefined.It is also common for us to have arrays of objects. In this case I would like to have an accessor attempted on an array automatically map across all of the array elements. Obviously while explicitly calling map in the expression is possible it can get very verbose. If
a
is an arraya.b
would become equivalent tomap(a, f(x) = x.a)
Thanks for the library!
Beta Was this translation helpful? Give feedback.
All reactions