Coder Social home page Coder Social logo

firstcodeoutlaw / plain-rules-engine Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 1.0 236 KB

A TypeScript library for creating and evaluating simple business rules in Node.js applications. It provides a flexible way to define and apply rules to your data

License: Apache License 2.0

JavaScript 0.20% TypeScript 99.80%
business-logic decision-engine logic-engine rule-engine rule-processor rules-engine conditional-processing data-rules rule-evaluation rules-framework type-safe-rules

plain-rules-engine's Introduction

Plain Rules Engine

License TypeScript

Introduction

Plain Rules Engine is a TypeScript library for creating and evaluating simple business rules in Node.js applications. It provides a flexible way to define and apply rules to your data.

Features

  • Easy rule creation and evaluation. Defining a rule in JSON is as simple as:
{
    "trackHasStrongLanguage": {
        "conditions": [
            ["$.user.age", "lessThan", 16],
            ["$.tags", "contains", "strong language"]
        ],
        "effect": {
            "action": "omit"
        }
    },
  ... other rules
}

You can also define rules in TypeScript:

export const musicSearchRules: Rule = {
  trackHasStrongLanguage: {
    conditions: [
      ['$.user.age', Operator.LESS_THAN, 16],
      ['$.tags', Operator.CONTAINS, 'strong language'],
    ],
    effect: {
      action: Action.OMIT,
    },
  },
  // Add more rules here
};

In this case, the effect defined for "trackHasStrongLanguage" will only be applied to objects that meet all conditions in conditions array

  • Customizable rule actions and conditions.
  • Written in TypeScript for type safety.

Installation

You can install Plain Rules Engine via npm:

npm install plain-rules-engine

Usage

Here's an example of how to use Plain Rules Engine:

import {
  RuleEngine,
  type Rule,
  type ApplyRulesResponse,
} from 'plain-rules-engine';

// Define your rules (could be in a JSON file, TS file, an object store, in-memory database etc.). See example rule definition above

// Fetch your rules (you'll have to create the function that does that)
const musicSearchRules: Rule = fetchRulesFromDataSource();

// Instantiate the rule engine
const engine = new RuleEngine(musicSearchRules);

// Define your data
const tracks = [
  {
    title: 'Heal the World',
    artist: 'Michael Jackson',
    year: 1991,
    album: 'Dangerous',
    tags: ['pop', 'society'],
  },
  {
    title: 'The Girl From Last Night',
    artist: 'Jackson Pepper',
    year: 2017,
    album: 'Slot Machine',
    tags: ['rap', 'strong language'],
  },
];

const user = {
  user: {
    age: 14,
    lastLogin: '2023-08-26T16:00:45Z',
  },
};

// Apply the rules
const evaluation = engine.applyRules(tracks, user);

// Handle the results
console.log('RESULT: ', evaluation);

For more detailed documentation and examples, please refer to Building a Rule Engine With TypeScript โ€” the article that inspired Plain Rules Engine.

Contribution

We welcome contributions from the community. If you'd like to contribute to this project or have any questions, issues, or suggestions, please feel free to open an issue.

License

This project is licensed under the Apache-2.0 License.

plain-rules-engine's People

Contributors

firstcodeoutlaw avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

eddwinpaz

plain-rules-engine's Issues

Effect value is number type only?

I'm trialling your library and I noticed when I add a rule with effect action of Add, the value can only be a number see line

value?: number;

Will you please enhance with a string type too?

Here's a code sample of my TypeScript error

effect: {
  action: Action.ADD,
  property: 'status',
  value: 'done' // must be a number
}

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.