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

boon-js

v2.0.5

Published

A parser and evaluator for boolean expressions written using the boon format

Downloads

2,896

Readme

boon-js

boon-js is a parser and evaluator for boon, The boolean expression language. This package is built in typescript and has no dependencies

Boon is a format for defining boolean expressions as strings. It looks like this:

# Checks if a film is one of the greatest blockbusters of all time!
isJurassicPark AND NOT (film_2 OR film_3)

These expressions can be:

  • stored in config such as JSON
  • shared between processes
  • written and read by non-technical team members or clients

Let me know how you are using boon-js by opening an issue on the github page. Happy to better support popular use cases.

Installation

Add this package from npm using npm install boon-js or yarn add boon-js

Usage

Use evaluateExpression to test any given input against an expression

import { evaluateExpression } from 'boon-js';

const expression = '(canOpenDoors OR isCleverGirl) AND hasLotsOfTeeth';

const mysteryAnimal = {
  canOpenDoors: true,
  isCleverGirl: false,
  hasLotsOfTeeth: true
};

evaluateExpression(expression, mysteryAnimal); // Returns true

API reference

evaluateExpression()

Tests any given input against an expression. Returns a boolean

Arguments

  • expression: string
  • booleanMap: Record<string, any>

Returns

  • boolean

Throws

  • Invalid token
  • Unexpected end of expression
  • Expected string but received [received]

getEvaluator()

Returns a function that evaluates the expression for any given input. The returned function takes a map of strings to any type. The type is coerced into a boolean to get the value of the string.

Arguments

  • expression: string

Returns

  • customEvaluateFunction

Throws

  • Invalid token
  • Unexpected end of expression
  • Expected string but received [received]

customEvaluateFunction()

Not exported directly. Returned from getEvaluator

Arguments

  • booleanMap: Record<string, any>

Returns

  • expressionResult: boolean

Throws

  • [received] should be an array. evaluate takes in a parsed expression. Use in combination with parse or use getEvaluator
  • Invalid token: [token]. Found in parsed expression at index 0
  • Invalid postfix expression: too many identifiers after evaluation

parse()

Parses an expression into a series of tokens in postfix notation

Arguments

  • expression: string

Returns

  • parsedExpression: PostfixExpression

Throws

  • Invalid token
  • Unexpected end of expression
  • Expected string but received [recieved]

evaluate()

Evaluates a PostfixExpression output from parse()

Arguments

  • expression: PostfixExpression
  • booleanMap: Record<string, any>

Returns

  • result: boolean

Throws

  • [received] should be an array. evaluate takes in a parsed expression. Use in combination with parse or use getEvaluator
  • Invalid token: [token]. Found in parsed expression at index 0
  • Invalid postfix expression: too many identifiers after evaluation

About boon

Boon is a standard format for human-readable boolean expressions. Typically, boolean expressions are defined within the code that evaluates them. Boon allows engineers to pull in a boolean expression defined elsewhere. This may be in another process or from a user interface

Boon supports the following operators:

  • NOT
  • XOR
  • AND
  • OR

Operators are evaluated in that order and must be uppercase. Boon also supports the use of parentheses to override operator precedence

The full boon specification is availble to view here

Examples

velociprator
# Use quotation marks where necessary
NOT "Tyrannosaurus Rex"
"Tyrannosaurus Rex" AND NOT (Brachiosaurus OR gallimimus)
"Tyrannosaurus Rex" AND (Brachiosaurus XOR (gallimimus OR T_PRORSUS))
# Boon supports annotations
tyrannosaurus-rex XOR (
  Brachiosaurus AND ( # Add comments to the end of any line
    gallimimus AND T_PRORSUS
  )
)

Under the hood

boon-js uses a lexer to produce a token stream from an expression string. This token stream is then fed to a parser. This parser uses Djikstra's shunting yard algorithm to convert the token stream to an array of tokens arranged using postfix notation. Once the identifiers are resolvable the evaluate function uses the postfix expression to compute a result

License

MIT