npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

boolean-parser

v0.0.2

Published

Script that parses a boolean query to an array with all the possibilities. IE: (a AND (b OR c)) -> [[a, b],[a, c]]

Downloads

234,422

Readme

Build Status Join the chat at https://gitter.im/riichard/boolean-parser-js

Boolean-parser

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.

Examples:

Input | Output -------- | --------- 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]]

Whereas a, b and 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

Input:
((a AND (b OR c)) AND (d AND e) AND (f OR g OR h)) OR i OR j
Output:
[[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]]

Philosophy

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.

Installing

npm install boolean-parser

Usage

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']]

Use cases

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 AND combination.

How does this library work

  1. 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.
  2. Go trough each string in that Array, and parse it to an array of AND items.
  3. Recursively call the parseBooleanQuery function on whatever is in between brackets. These recursive calls will return an array of all possible combinations within those brackets. (An OR array of AND combinations)
  4. 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] ] ]
  1. 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] ] ]
  1. 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 ] ]
]

Out:

[
    [ a, c, d, f ],
    [ b, c, d, f ],
    [ a, e, f ],
    [ b, e, f ]
]
  1. Then we concatenate all those OR paths that were in between those OR terms to one Array using the mergeOrs function. In:
[
    [ [ a, b ], [ c ] ],
    [ [ d ] ],
    [ [ e ], [ f, g ] ]
]

Out:

[
    [ a, b ], [ c ], [ d ], [ e ], [ f, g ]
]