boon-js
TypeScript icon, indicating that this package has built-in type declarations

2.0.5 • Public • Published

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

Package Sidebar

Install

npm i boon-js

Weekly Downloads

444

Version

2.0.5

License

MIT

Unpacked Size

62.6 kB

Total Files

47

Last publish

Collaborators

  • jakec-npm