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

1.0.7 • Public • Published

verdict

A lightweight decision tree/rules engine.

Features

  • Support for complex branching logic
  • Support for and/or boolean logic
  • Wide range of operators supported
  • Support for "fallback" values so paths don't "fall through the cracks" once an initial condition is met

Installation

Install with yarn:

yarn add verdict

Usage

import { DecisionTree, Operator } from 'verdict';
 
// Create a new DecisionTree
const tree = new DecisionTree({
  condition: { path: 'quux', operator: Operator.Equals, value: 'bar' },
});
 
// Start by adding a child to the tree. Because the child doesn't specify a
// `value`, it's assumed to be a "branch" node which means that it's expected to
// have one or more children (at some point).
const child = tree.addChild({
  condition: {
    path: 'foo.bar',
    operator: Operator.Equals,
    value: 'baz',
  },
});
 
// Follow the same process for adding another descendent (grandchild in this
// case). Note that it's not necessary to have this as a separate step. We
// easily could have included a `children` property within our first payload
// that we passed to `addChild` and included the grandchild there.
child.addChild({
  condition: {
    path: 'foo.baz',
    operator: Operator.ContainsSubstring,
    value: 'quux',
  },
  // Because the child has a `value` field, it's considered a leaf node.
  value: `Yay! I'm a leaf node!`,
});
 
const data = {
  quux: 'bar',
  foo: {
    bar: 'baz',
    baz: 'oOoQuuXoOo',
  },
};
 
console.log(tree.evaluate(data)); // Yay! I'm a leaf node!

If you're not interested in creating a decision tree, you can import Rule from the package and use it on its own.

Inspiration

This library was inspired by verdict.js.

Readme

Keywords

none

Package Sidebar

Install

npm i verdict

Weekly Downloads

16

Version

1.0.7

License

MIT

Unpacked Size

24.6 kB

Total Files

19

Last publish

Collaborators

  • vincecoppola