UNPKG

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).

33 lines (32 loc) 1.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ohm_js_1 = require("ohm-js"); var grammar_1 = require("./grammar"); var util_1 = require("./util"); var evaluator_1 = require("./evaluator"); var BooleanExpressions = /** @class */ (function () { function BooleanExpressions(exp) { var truthGrammar = ohm_js_1.grammar(grammar_1.grammarRules); var matchResult = truthGrammar.match(exp); if (matchResult.failed()) { throw new Error("Parse failed " + matchResult.shortMessage); } this.variableNames = util_1.extractVariables(exp); this.evaluateFunc = evaluator_1.createSemantics(truthGrammar, matchResult); } /** * Returns a list of all the unique variable names in the boolean expression. */ BooleanExpressions.prototype.getVariableNames = function () { return this.variableNames; }; /** * Evaluates the given boolean expression with the given variables set to true * and the omitted variables set to false. */ BooleanExpressions.prototype.evaluate = function (variables) { return this.evaluateFunc(variables); }; return BooleanExpressions; }()); exports.default = BooleanExpressions;