@schoenbergerb/logical-expression-parser
v1.1.7
Published
Logical expression parser and evaluates result, suitable for permissions management
Downloads
4
Maintainers
Readme
Logical Expression Parser
This is a logical expression parser for TypeScript, it can parse a logical expression into a AST object and evaluates the result using your token checking function.
It's original developed by NimitzDEV: https://github.com/NimitzDEV/logical-expression-parser
Improvements compared to the original:
- typescript support
- custom logical operators
[Default] logical operators
/
Or&
And!
Not()
Parentheses
How it works
- The parser parse and tokenize the expression, for example one of your function requires
REGISTERED&(SPECIAL/INVITED)
- Parser then will pass
REGISTERED
,SPECIAL
andINVITED
into your token checking function to get a boolean result - Finaly the parser will evaluates the final result
Examples
const { evaluate } = ExpressionParserFactory.init().build();
const result1 = evaluate("REGISTERED&(SPECIAL/INVITED)", ["REGISTERED", "INVITED"]); // --> true
const result2 = evaluate("REGISTERED&(SPECIAL/INVITED)", ["REGISTERED"]); // --> false
use custom operators if needed:
const { evaluate } = ExpressionParserFactory.init()
.setToken(Token.OPERATOR_AND, '+')
.setToken(Token.OPERATOR_NOT, '-')
.setToken(Token.OPERATOR_OR, '|')
.setToken(Token.PARENTHESES_OPEN, '{')
.setToken(Token.PARENTHESES_CLOSE, '}')
.build();
const result = evaluator.evaluate("{A+C}|{B&-C}", ['A', 'B'])