boolean-expressions
Version:
Boolean expression parser and evaluator. Makes use of the Ohm grammar package with grammar rules found [here](https://github.com/avadavat/boolean-expressions/blob/master/src/grammar/grammarRules.ts).
35 lines (22 loc) • 1.26 kB
Markdown
# boolean-expressions
Boolean expression parser and evaluator. Makes use of the Ohm grammar package with grammar rules found [here](https://github.com/avadavat/boolean-expressions/blob/master/src/grammar/grammarRules.ts).
[](https://www.npmjs.com/package/boolean-expressions)

## Installation
The package is hosted on npm [here](https://www.npmjs.com/package/boolean-expressions). Simply install with `npm install boolean-expressions`.
## Usage
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']);
```