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

Cannot use '&&' in expression #253

Open
gkjohnson opened this issue Apr 23, 2021 · 2 comments
Open

Cannot use '&&' in expression #253

gkjohnson opened this issue Apr 23, 2021 · 2 comments

Comments

@gkjohnson
Copy link

When declaring '&&' in the "binaryOps" field an error is thrown when parsing a binary '&&' expression:

import { Parser } from 'expr-eval';

const parser = new Parser();
parser.binaryOps[ '&&' ] = ( a, b ) => Boolean( a && b );
parser.evaluate( '1 && 1' );

// error!

Thanks for the awesome library!

@silentmatt
Copy link
Owner

Unfortunately, the binaryOps object can't be used to add new operators, only change the implementation of existing ones. It's kind of confusing because you can add to unaryOps (with the limitation that the operator needs to be a valid identifier, so no special characters). It's something that would be nice to support in the future, but isn't currently because of how the parser works.

@gkjohnson
Copy link
Author

Got it -- makes sense. Reworking the parsing for unary ops sounds like a large task. As a smaller adjustment it might be nice to add support for just && as an empty / non-implemented operator so users can override the behavior considering it's frequently used as boolean AND. It could throw an error by default so people don't mistakenly use it without it being overwritten.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants