Git Product home page Git Product logo

verdict's Introduction

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.

verdict's People

Contributors

vinsidious avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.