https://github.com/nimitzdev/logical-expression-parser
Logical expression parser written in JavaScript
https://github.com/nimitzdev/logical-expression-parser
Last synced: 12 months ago
JSON representation
Logical expression parser written in JavaScript
- Host: GitHub
- URL: https://github.com/nimitzdev/logical-expression-parser
- Owner: NimitzDEV
- License: mit
- Created: 2018-12-06T12:29:43.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-04-21T16:35:16.000Z (about 3 years ago)
- Last Synced: 2025-05-07T04:48:50.283Z (12 months ago)
- Language: JavaScript
- Size: 9.77 KB
- Stars: 22
- Watchers: 1
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Logical Expression Parser
[](https://badge.fury.io/js/logical-expression-parser)
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
1. `&` And
1. `!` Not
1. `()` Parentheses
## How it works
1. The parser parse and tokenize the expression, for example one of your function requires `REGISTED&(SPECIAL|INVITED)`
1. Parser then will pass `REGISTED`, `SPECIAL` and `INVITED` into your token checking function to get a boolean result
1. Finaly the parser will evaluates the final result
## Example
```javascript
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
```