https://github.com/thipages/bparser
A boolean expression parser
https://github.com/thipages/bparser
boolean expression parser
Last synced: 10 months ago
JSON representation
A boolean expression parser
- Host: GitHub
- URL: https://github.com/thipages/bparser
- Owner: thipages
- License: mit
- Created: 2021-03-28T15:04:49.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-03-29T08:36:25.000Z (almost 5 years ago)
- Last Synced: 2025-03-22T04:03:18.869Z (10 months ago)
- Topics: boolean, expression, parser
- Language: JavaScript
- Homepage:
- Size: 7.81 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bparser
A boolean (OR, AND) expression parser
## Installation
- via npm : _npm i bparser_
```javascript
import bparser from 'bparser';
// then
bparser(expression,regExp);
```
- via HTML
```html
// then
bparser(expression,regExp);
```
## Usage
```javscript
const pathsObjet = bparser(expression,regExp);
```
`expression` : String containing
- comparison operator(s) : `AND,OR`
- with optional opening/closing parentheses `(,)`
- with items to be compared
`regExp` : RegExp allowing to identify the items to be compared
`returns` an object with the following properties
- `isValid`, a boolean for a valid/invalid expression
- `results`, an array (`OR` list) of the different `AND` paths or an empty array of `isValid` is `false`
## Examples
RegExp: `/[a-z]/g`
Expression | Results
-------- | ---------
`a AND b` | `[[a, b]]`
`a OR b` | `[[a], [b]]`
`a AND b AND c` | `[[a, b, c]]`
`a AND b OR c` | `[[a, b], [c]]`
`a AND (b OR c)` | `[[a, b], [a, c]]`
`a AND (b OR c) AND (d OR e)` | `[[a, b, d], [a, b, e], [a, c, d], [a, c, e]]`
RegExp: `/'.+?'/g`
Expression | Results
-------- | ---------
`"'a' AND 'b' OR 'c'" ` | `["'a'","'b'"],["'c'"]`
### More complex example
###### Regex : `/[a-z]/g`
###### Expression:
```
((a AND (b OR c)) AND (d AND e) AND (f OR g OR h)) OR i OR j
```
###### Results:
```
[[a,b,d,e,f],
[a,c,d,e,f],
[a,b,d,e,g],
[a,c,d,e,g],
[a,b,d,e,h],
[a,c,d,e,h],
[i],
[j]]
```
## Related projects
### [Boolean-parser](https://github.com/riichard/boolean-parser-js)
The majority of the examples comes from this other (and fun) implementation
## Algorithm
The algorithm heavily relies on String and Regex.
Even if the performance is slightly better than [Boolean-parser](https://github.com/riichard/boolean-parser-js), I think it can be dramatically improved considering:
- one way analysis of the expression instead of two
- better data structure for keeping states and merging them
This may have also an favorable impact on the LOC number.
... to be continued