Boolean expression parser and evaluator. Makes use of the Ohm grammar package with grammar rules found here.
The package is hosted on npm here. Simply install with npm install boolean-expressions
.
This package provides a BooleanExpressions
class which parses an expression once and allows an unlimited number of evaluations on that expression.
To parse an expression, instantiate a new instance of the BooleanExpressions
class with the given expression. For example,
const b = new BooleanExpressions('p and q or t');
To get the unique variables from that query, call:
// Would return ['p', 'q', 't']
const variables = b.getVariableNames();
To evaluate the expression with the given variables set to true, use:
// Evaluates the expression with p=true, q=true, and t=false
const result = b.evaluate(['p', 'q']);