TomFrost / Jexl

Javascript Expression Language: Powerful context-based expression parser and evaluator
MIT License
561 stars 92 forks source link

No unary plus? #59

Closed Kamikadze4GAME closed 4 years ago

Kamikadze4GAME commented 5 years ago

Why is there only one unary operator (!)? Unary plus is a common way to convert to number (like +[] === 0).

TomFrost commented 4 years ago

Currently, Jexl has a limitation in that you can't use a symbol as both a binary and a unary operator. This was a design decision I made early on to simplify and separate the Lexer from the Parser, but I keep revisiting that decision -- mostly because the - sign should also be a unary operator, and requires intelligent logic in the lexer to determine when it's being used to negate a number versus as a binary subtraction operator.

With that said: The + symbol's function as a simple way to convert to numbers is one I'd be hesitant to add to Jexl. While Jexl takes many of its expression language features from Javascript, I try to be very discerning taking only the pieces that offer good intuitive design no matter what language you're ultimately using. Jexl has been ported to various other scripting languages where + does not have that functionality, so for that use case a userland transform like |toNum would be more portable and readable.