This function converts a boolean query to a 2 dimensional array with all possibilities. This allows you to quickly and easily see what scenarios will equal true in a complex boolean conditional.
c represent words, forming a complex query pattern.
This function works recursively trough all brackets and generates an array of all possible combinations of a matching query.
Long term example
((a AND (b OR c)) AND (d AND e) AND (f OR g OR h)) OR i OR j
[[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]]
The output is meant to be easily parsed to check for matches. There are more efficient ways to check matches to this query by only checking each term once, though this method is one that is easier to maintain and limits risk of side effects. Especially when considering recursively nested queries involving many brackets and AND/OR combinations.
npm install boolean-parser
var booleanParser = require('boolean-parser'); var searchPhrase = '((a AND (b OR c)) AND (d AND e) AND (f OR g OR h)) OR i OR j'; var parsedQuery = booleanParser.parseBooleanQuery(searchPhrase); // Returns: // [['a','b','d','e','f'], // ['a','b','d','e','g'], // ['a','b','d','e','h'], // ['a','c','d','e','f'], // ['a','c','d','e','g'], // ['a','c','d','e','h'], // ['i'],['j']]
This tool is great when constructing complex search filters that need to be matched with text. Creating search tools that need to show up more refined results than that would be possible with a simple
How does this library work
- Parse string to an Array of OR items (strings). Everything that's in between brackets will be treated as one word and will later be recursively parsed.
- Go trough each string in that Array, and parse it to an array of AND items.
- Recursively call the
parseBooleanQueryfunction on whatever is in between brackets. These recursive calls will return an array of all possible combinations within those brackets. (An
- Create an empty array called
nestedPaths. And add all nested combinations that are in between brackets to that array. For instance, with the query:
((a AND (b OR c)) AND (d AND e) AND (f OR g OR h OR j)) AND x AND y AND z
Path will look like the following.
// nestedPath = [ [ [a,b], [a,c] ], [ [d,e] ], [ [f], [g], [h], [j] ] ]
- Then push the remaining non-bracket AND terms to this array.
// nestedPath = [ [ [a,b], [a,c] ], [ [d,e] ], [ [f], [g], [h], [j] ] [ [x,y,z] ] ]
- Then using the
orsAndMerge, all those AND paths in those OR paths will be combined with the other OR combinations. In:
[ [ [ a ], [ b ] ], [ [ c, d ], [ e ] ], [ [ f ] ] ]
[ [ a, c, d, f ], [ b, c, d, f ], [ a, e, f ], [ b, e, f ] ]
- Then we concatenate all those
ORpaths that were in between those
ORterms to one Array using the
[ [ [ a, b ], [ c ] ], [ [ d ] ], [ [ e ], [ f, g ] ] ]
[ [ a, b ], [ c ], [ d ], [ e ], [ f, g ] ]