verdict

A lightweight rules engine/decision tree

Usage no npm install needed!

<script type="module">
  import verdict from 'https://cdn.skypack.dev/verdict';
</script>

README

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.