expr-eval icon indicating copy to clipboard operation
expr-eval copied to clipboard

Cannot use '&&' in expression

Open gkjohnson opened this issue 3 years ago • 2 comments

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!

gkjohnson avatar Apr 23 '21 21:04 gkjohnson

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.

silentmatt avatar Apr 27 '21 14:04 silentmatt

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!

gkjohnson avatar Apr 27 '21 16:04 gkjohnson