npm.io
0.1.1 • Published 5 years ago

bparser

Licence
MIT
Version
0.1.1
Deps
0
Size
54 kB
Vulns
0
Weekly
0
Stars
2

bparser

A boolean (OR, AND) expression parser

Installation

  • via npm : npm i bparser
import bparser from 'bparser';
// then
bparser(expression,regExp);
  • via HTML
<script src="min.js"></script>
<script>
    // then
    bparser(expression,regExp);
</script>

Usage

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]]
Boolean-parser

The majority of the examples comes from this other (and fun) implementation

Keywords