3.1.2 โ€ข Published 10 months ago

@arunkumar_h/rule-engine v3.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

@arunkumar_h/rule-engine

Known Vulnerabilities NPM Version NPM Downloads License Bundle Size Install size

badge-branches badge-functions badge-lines badge-statements


Breaking Change: Please move to v3.1.0 or later.


A lightweight and extensible rule engine built with TypeScript and Node.js. Define complex business rules and evaluate conditions easily using a simple JSON structure.

๐Ÿ“ฆ Installation

npm install @arunkumar_h/rule-engine
yarn add @arunkumar_h/rule-engine

๐Ÿง  Features

  • โœ… Logical condition support (and, or, nested expressions)
  • ๐Ÿ”ง Custom operators and named conditions
  • ๐Ÿ“œ Fully typed with TypeScript
  • ๐Ÿš€ Lightweight and dependency-aware
  • ๐Ÿ”Ž Native JMESPath support for data querying
  • ๐Ÿงฐ Built-in caching using lru-cache for better performance
Feature / Capability@arunkumar_h/rule-engine
โœ… Written in TypeScriptโœ… Native TypeScript with full type safety
โš™๏ธ Custom Operatorsโœ… Built-in support, sync or async
๐Ÿง  Named Conditionsโœ… Supports reusable named conditions
๐Ÿงฑ Nested Logical Treesโœ… Fully supported (and, or, deeply nested)
๐Ÿ” Data Query Languageโœ… Built-in JMESPath support
๐Ÿš€ Performance Optimizationsโœ… Rule-level cache with lru-cache
๐Ÿงฐ Extensibilityโœ… Add custom operators, conditions dynamically
โš–๏ธ Lightweightโœ… Small and focused build
๐Ÿงช Testing Coverage Readyโœ… Easy to unit test each rule block
๐Ÿ” Dynamic Rule Loadingโœ… Add/modify rules at runtime
๐Ÿ”„ Async Supportโœ… Full async engine and operators
๐Ÿ“ฆ Modern Packagingโœ… ESM + CJS + .d.ts types out of the box

โš™๏ธ Default Operators

The following operators are available by default:

OperatorDescription
===Strict equality
!==Strict inequality
==Loose equality
!=Loose inequality
>Greater than
>=Greater than or equal to
<Less than
<=Less than or equal to
%likeStarts with
like%Ends with
%like%Contains
inValue is in the array
!inValue is not in the array
includesArray includes value
!includesArray does not include value

๐Ÿ”จ Basic Usage

  • condition This containes and and or as main block.
  • onSuccess value that will be returned or function that will be invoked if the condition is satisfied.
  • onFail value that will be returned or function that will be invoked if the condition fails.
  • cache as default this will be set to true and can be disabled for rule wise false
import { Engine } from "@arunkumar_h/rule-engine";

const engineObj = new Engine();
const rule = {
  testRule: {
    condition: {
      and: [
        { path: "age", operator: "!==", value: 10 },
        {
          and: [
            { path: "age", operator: ">", value: 15 },
            {
              or: [
                { path: "age", operator: "!==", value: 30 },
                { path: "skills", operator: "includes", value: "ts" },
              ],
            },
          ],
        },
        { path: "language", operator: "in", value: ["tamil", "english"] },
      ],
    },
    onSuccess: (fact, ruleName) => "Success", // onSuccess: { id: 23 }
    onFail: (fact, ruleName) => "Fail", // onFail: "Error"
    cache: false, // default will be true
  }
};
engine.addRule(rule);

const fact = {age: 16, skills: ["ts", "php"], language: "tamil"}; // Your data to be validated 
const result = await engineObj.run(fact, "testRule");

๐Ÿ”ง Custom Operator Example

engine.addOperator({
  isEven: (factValue) => factValue % 2 === 0,
});

const rule = {
  evenCheck: {
    condition: {
      and: [
        { path: "number", operator: "isEven" },
      ],
    },
    onSuccess: "Number is even",
    onFail: "Number is odd",
  },
};

const result = await engine.run({ number: 8 }, "evenCheck");

๐Ÿ” API Overview

flowchart TB
    Rule --> onSuccess
    Rule --> onFail
    Rule --> Condition --> AND --> Operation
    Condition --> OR --> Operation

Engine API

let engine = new Engine() 

addRule({ rule1, rule2, ... })

  • Add named rules dynamically.

addCondition({ condition1, condition2, ... })

  • Add reusable named conditions.
  • Conditions can reference other named conditions.

addOperator({ customOperator1, customOperator2, ... })

  • Add custom (sync or async) operators.

run(fact, ruleName)

  • Executes a given rule against the provided fact

โšก Advanced Usage

  • Adding named conditions.
  • Adding named operators.
  • Rule wise cache disabling.
import { Engine } from "@arunkumar_h/rule-engine";

const engineObj = new Engine();

const condition1 = {
  condition1: {
    and: [
      { path: "age", operator: "!==", value: 10 },
      {
        and: [
          { path: "age", operator: ">", value: 15 },
          {
            or: [
              { path: "age", operator: "!==", value: 30 },
              { path: "skills", operator: "includes", value: "ts" },
            ],
          },
        ],
      },
      { path: "language", operator: "in", value: ["tamil", "english"] },
    ],
  }
};
engine.addCondition(condition1);  // adding named condition

const rule = {
  testRule: {
    condition: "condition1",  // Using named condition
    onSuccess: "Success",  //  can be a function or a data
    onFail:  "Fail", //  can be a function or a data
    cache: false  // disable cache for this rule 
  }
};
engine.addRule(rule);

const fact = {age: 16, skills: ["ts", "php"], language: "tamil"}; // Your data to be validated 
const result = await engineObj.run(fact, "testRule");

๐Ÿงช Test Coverage

Badges above represent live coverage stats for:

  • badge-branches
  • badge-functions
  • badge-lines
  • badge-statements

Author

Arunkumar H

LinkedIn GitHub Email

๐Ÿ“„ License

Some non-code content (e.g. diagrams, images, markdown docs) is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.

See https://creativecommons.org/licenses/by-sa/4.0/ for more info.

The detailed list of Open Source dependencies can be found in Fossa report.

3.1.2

10 months ago

3.1.1

10 months ago

3.1.0

10 months ago

3.0.2

10 months ago

3.0.1

10 months ago

3.0.0

10 months ago

2.0.0

10 months ago

2.0.0-dev.2

10 months ago

2.0.0-dev.1

10 months ago

1.6.0-dev.1

10 months ago

1.5.2-dev.2

10 months ago

1.5.2-dev.1

10 months ago

1.0.0-dev.2

10 months ago

1.0.0-dev.1

10 months ago

1.5.1

10 months ago

1.5.0

10 months ago

1.4.1

10 months ago

2.0.0-dev.0

10 months ago

1.4.0

10 months ago

1.3.0

10 months ago

1.2.5

10 months ago

1.2.4

10 months ago

1.2.3

10 months ago

1.2.2

10 months ago

1.2.1

10 months ago

1.2.0

10 months ago

1.1.0

10 months ago

1.0.20-2

10 months ago

1.0.19-0

10 months ago

1.0.18

10 months ago

1.0.17

10 months ago

1.0.16

10 months ago

1.0.15

10 months ago

1.0.14

10 months ago

1.0.13

10 months ago

1.0.12

10 months ago

1.0.11

10 months ago

1.0.10

10 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago