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

40 lines (39 loc) 1.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); // Splits by parentheses, white space, and special characters. var splitRegex = new RegExp(/[ ()|&~!^]+/g); var reservedKeywords = new Set([ "and", "not", "or", "xor", "if", "then", "iff", "only", "true", "false", "equals", "imply", "implies", "nimply", "nimplies", "xnor", "nand", "nor" ]); /** * Returns a list of all the unique variable names in the given expression. */ function extractVariables(expression) { var words = expression.split(splitRegex); // Any word that isn't a reserved keyword is a variable. var variables = words .filter(function (word) { return word.length > 0; }) .filter(function (word) { return /^[A-Za-z0-9]+$/.test(word); }) .filter(function (word) { return !reservedKeywords.has(word.toLowerCase()); }); // Only count each variable once. var uniqueVariables = Array.from(new Set(variables)); return uniqueVariables; } exports.default = extractVariables;