UNPKG

@schoenbergerb/logical-expression-parser

Version:

Logical expression parser and evaluates result, suitable for permissions management

47 lines 1.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PolishGenerator = exports.PolishNotation = void 0; const token_type_1 = require("./token-type"); const PolishNotation = (tokens, customTokens) => { const queue = []; const stack = []; tokens.forEach((token) => { const type = (0, token_type_1.getTokenTypeByValue)(token.value, customTokens); switch (type) { case token_type_1.Token.LITERAL: queue.unshift(token); break; case token_type_1.Token.OPERATOR_AND: case token_type_1.Token.OPERATOR_OR: case token_type_1.Token.OPERATOR_NOT: case token_type_1.Token.PARENTHESES_OPEN: stack.push(token); break; case token_type_1.Token.PARENTHESES_CLOSE: while (stack.length && stack[stack.length - 1].type !== token_type_1.Token.PARENTHESES_OPEN) { const i = stack.pop(); i && queue.unshift(i); } stack.pop(); if (stack.length && stack[stack.length - 1].type === token_type_1.Token.OPERATOR_NOT) { const i = stack.pop(); i && queue.unshift(i); } break; default: break; } }); const result = (stack.length && [...stack.reverse(), ...queue]) || queue; return result; }; exports.PolishNotation = PolishNotation; const PolishGenerator = function* (polish) { for (let index = 0; index < polish.length - 1; index++) { yield polish[index]; } return polish[polish.length - 1]; }; exports.PolishGenerator = PolishGenerator; //# sourceMappingURL=polish.js.map