1.1.7 • Published 2 years ago

@schoenbergerb/logical-expression-parser v1.1.7

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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

  1. The parser parse and tokenize the expression, for example one of your function requires REGISTERED&(SPECIAL/INVITED)
  2. Parser then will pass REGISTERED, SPECIAL and INVITED into your token checking function to get a boolean result
  3. 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'])
1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago