silentmatt / expr-eval

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

Improved how expressions are simplified to eliminate sub-expressions that can be evaluated #173

Open danielmittelman opened 6 years ago

danielmittelman commented 6 years ago

This pull request changes how the simplify() function works when dealing with expressions that can be simplified even further.

Example 1:

true and true or true

Before the change, this would have evaluated to something like true and (true or (true)). Yet, since this is an expression that can already be simplified to a single literal, it now evaluates to:

true

Example 2:

(5 > 2) or x > 8

Now, when running simplify(), this will evaluate to true or (x > 8). When running simplify({x: 10}), this will evaluate to true.

silentmatt commented 6 years ago

This is really cool. I'll take a look at it and try to get this merged for the next release.