silentmatt / expr-eval

Mathematical expression evaluator in JavaScript
http://silentmatt.com/javascript-expression-evaluator/
MIT License
1.19k stars 239 forks source link

Chaining if functions #191

Closed jpike88 closed 5 years ago

jpike88 commented 5 years ago

I have a bunch of if statements to evaluate like an if...else if... else statement.

Would be great to be able to do something like this, instead of nesting loads of if statements:

if(x, y).else(a, b).else(z)

queequac commented 5 years ago

Why not ternary operator, which is available as of today? x ? y : a ? b : z or for better readability x ? y : (a ? b : z)

Sure, this is still nested... but chaining does not make sense from my point of view, since the return value would have to be something you can only invoke else-statements on. If-else as fluent API looks - aside from readability - not very logical and expectable to me.

silentmatt commented 5 years ago

First of all, thanks for the suggestion. I agree with @queequac though. The if function was from before the ternary operator was supported, and really only still exists for backward compatibility. Chaining if/else calls looks kind of cool, but I don't think the complexity needed to support that is worth it for something that's probably going away eventually.

jpike88 commented 5 years ago

Damn, I have expressions that chain up to 10 ternary operators already.