Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluate expression with strings #244

Open
mabregana5 opened this issue Jan 4, 2021 · 3 comments
Open

Evaluate expression with strings #244

mabregana5 opened this issue Jan 4, 2021 · 3 comments
Labels

Comments

@mabregana5
Copy link

mabregana5 commented Jan 4, 2021

Is there a way to evaluate an expression and concatenate strings in between?

For example:

let expr = "'' + (datediff('mm', a, b) - datediff('yy', a, b) * 12) + ' years'";
let params = { a: moment(), b: moment({ y: 2020, m: 1, d: 1 }) };
let value = parser.parse(expr).evaluate(params)

Expected:
value == "[dateValue] years"

Actual:
value == NaN

We have a form of expression evaluation in our web app, but we are using this package within our more recent PWA. The example expression in expr is one way we can add strings and have substituted values in the formulas.

value is assigned NaN, but if you remove the strings, it is instead assigned with an appropriate value.

Originally posted by @mabregana5 in #240 (comment)

@silentmatt
Copy link
Owner

The expression language (unlike javascript) uses || for string concatenation (borrowed from SQL). + will always try to convert its operands to numbers first, which is why you're getting NaN.

let expr = "'' || (datediff('mm', a, b) - datediff('yy', a, b) * 12) || ' years'";
let params = { a: moment(), b: moment({ y: 2020, m: 1, d: 1 }) };
let value = parser.parse(expr).evaluate(params)

@mabregana5
Copy link
Author

Thanks for the reply @silentmatt.

I was wondering if you had any suggestions in the ability to substitute the '+' operator with the double pipe '||' in sub-expressions that involve string concatenation?

Or a way to override the '+' operator for custom logic, as opposed to adding a custom function?

@silentmatt
Copy link
Owner

Substituting the operator would be tricky, since the expressions don't store any type information, but you can override the + operator implementation by replacing the parser.binaryOps['+'] function with your own. If you want JavaScript's behavior, it can be as simple as:

let parser = new Parser();
parser.binaryOps['+'] = (a, b) => a + b;

This was referenced Jan 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants