Overriding function using math.import does not override that function when using evaluate function #2651
-
UPDATE - I made some progress and am able to avoid the error described in this comment, however I am still receiving incorrect output when using evaluate() -- Hi josdejong! I am enjoying using your library. Background I am working on adding the ability to create user-defined calculated columns to a grid of data. I am using MathJS to evaluate an expression in the form of a string representing the calculated column calculation. This string is defined by the user.
I am having an issue regarding the treatment of null values causing errors when evaluating the expression. Some rows of data may not have a property which is included in the expression, causing an error when evaluate() is called. I did some research on MathJS treatment of null values and I did find #830 so I understand why I am encountering these issues. My goal is to have expressions with null values in them treated as excel treats them. For example during simple mathematical operations I would like a null value to be treated as 0 (eg 5 + null -> 5 + 0 = 5), and in functions I would like them to be omitted (eg mean(5, 10, null) = 7.5 rather than 5) Question I found #1829 where someone had a similar issue regarding the treatment of null in functions. I followed the pattern laid out by @Big-Gremlin (#1829 (comment)) but it seems to only work when explicitly calling the overridden function, and does not work when that function comes up in .evaluate(expression, scope).
My current solution is to detect if the value is null, and if so set it as 0 in the evaluate() scope. This handles the math operator side of the problem (+, -, /, *, etc). Then my next step if I cannot figure this out is to parse the string expression for each function and modify the string to remove the null property from the function parameters. This seems a little hacky to me so I am trying to avoid it. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
UPDATE I was able to get the function override to run in the evaluate() function without causing errors by modifying a part of the import command in the solution created by Big-Gremlin
However, the output is not correct. After applying the override, evaluate() always returns the first parameter rather than the correct calculated output:
In the following code I placed a breakpoint and noticed that when using math.max() x is an array with the values of the parameters inside. When using evaluate('max()') x is a singular value representing only the first parameter of the max function. Is there a different technique I need to use to gain access to all the parameters and filter out the null values when using evaluate()?
|
Beta Was this translation helpful? Give feedback.
-
The max function accepts both an array like const defaultMaxFunc = mathjs.max;
const newMaxFunc = factory('max', [], () => {
return (...args) => {
// args is always an array, and can hold either a list with singular values, or a single array or Matrix
// ... adjust null values here...
return defaultMaxFunc.apply(null, args);
};
}); |
Beta Was this translation helpful? Give feedback.
The max function accepts both an array like
max([1, 2, 3])
or variable arguments likemax(1, 2, 3)
. In your example you use both notations. Your custom version works formax([a, b, c])
, where you pass one argument which is an array, but what your custom version is missing is handling the case where a user entersmax(a, b, c)
: you only look at the first argument. To fix this, your implementation should do something like: