Skip to content

NimitzDEV/logical-expression-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logical Expression Parser

npm version

This is a logical expression parser for JavaScript, it can parse a logical expression into a AST object and evaluates the result using your token checking function.

Supported logical operators

  1. | Or
  2. & And
  3. ! Not
  4. () Parentheses

How it works

  1. The parser parse and tokenize the expression, for example one of your function requires REGISTED&(SPECIAL|INVITED)
  2. Parser then will pass REGISTED, SPECIAL and INVITED into your token checking function to get a boolean result
  3. Finaly the parser will evaluates the final result

Example

const LEP = require('logical-expression-parser');

const REQUIREMENTS = 'REGISTED&(SPECIAL|INVITED)';
const LIST_A = ['REGISTED', 'INVITED'];
const LIST_B = ['SPECIAL', 'EXPERT'];

const RESULT_A = LEP.parse(REQUIREMENTS, t => LIST_A.indexOf(t) > -1);
const RESULT_B = LEP.parse(REQUIREMENTS, t => LIST_B.indexOf(t) > -1);

// RESULT_A: true
// RESULT_B: false

About

Logical expression parser written in JavaScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published