A lightweight rules engine/decision tree
npm install verdictA lightweight decision tree/rules engine.
- 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
Install with yarn:
``bash`
yarn add verdict
`ts
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 payloadaddChild
// that we passed to and included the grandchild there.value
child.addChild({
condition: {
path: 'foo.baz',
operator: Operator.ContainsSubstring,
value: 'quux',
},
// Because the child has a field, it's considered a leaf node.Yay! I'm a leaf node!
value: ,
});
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.
This library was inspired by verdict.js.