@arunkumar_h/rule-engine
TypeScript icon, indicating that this package has built-in type declarations

3.1.0 • Public • Published

@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:

Operator Description
=== Strict equality
!== Strict inequality
== Loose equality
!= Loose inequality
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
%like Starts with
like% Ends with
%like% Contains
in Value is in the array
!in Value is not in the array
includes Array includes value
!includes Array 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.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
2.0.0-dev.27dev
3.1.00latest

Version History

VersionDownloads (Last 7 Days)Published
3.1.00
3.0.20
3.0.139
3.0.043
2.0.045
2.0.0-dev.27
2.0.0-dev.16
1.6.0-dev.16
1.5.2-dev.26
1.5.2-dev.16
1.0.0-dev.26
1.0.0-dev.16
1.5.110
1.5.07
1.4.16
2.0.0-dev.05
1.4.06
1.3.05
1.2.54
1.2.43
1.2.33
1.2.23
1.2.12
1.2.02
1.1.02
1.0.20-22
1.0.19-02
1.0.182
1.0.171
1.0.161
1.0.151
1.0.141
1.0.131
1.0.121
1.0.111
1.0.101
1.0.91
1.0.81
1.0.61
1.0.51

Package Sidebar

Install

npm i @arunkumar_h/rule-engine

Weekly Downloads

246

Version

3.1.0

License

MIT

Unpacked Size

35.4 kB

Total Files

8

Last publish

Collaborators

  • arunkumar_h