npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@aipeel/json-rules-engine

v1.0.1

Published

## What does it do? Pass a Rule and Data to engine and get a synchronous response on whether the Data satisfies the Rules.

Downloads

12

Readme

JSON Rule Engine

What does it do?

Pass a Rule and Data to engine and get a synchronous response on whether the Data satisfies the Rules.

@aipeel/json-rules-engine

Installation

    npm install @aipeel/json-rules-engine

Usage

Step 1: Define Rules JSON

  1. Rules JSON must be an object

  2. Must start with AND or OR which takes array of rule

    1. Single Rule Criteria
    {
        AND: [ Criteria1 ]
    }
    1. Multiple Rule Criteria --> Criteria 1 and 2 must be satisfied
    {
        AND: [ Criteria1, Criteria2 .. ]
    }
    1. Multiple Levels of Rules --> Criteria 1 & 2 must be satisfied and either Criteria3 or Criteria4 must be satisfied
    {
        AND: [
                {
                    AND: [ Criteria1, Criteria2 ...]
                },
                {
                    OR: [Criteria3, Criteria4 ..]
                }
            ]
    }
  3. Each Criteria must be an object

    1. The key in criteria is an operator and value defines the metadata required for the operator to workExamples of Criteria

      { 
          "between": { 
                          "min": "10", 
                          "max": "20", 
                          "attribute": "discount" 
                      }
      }
      { 
          "in": { 
                  "value": ["C","Javascript","Python"], 
                  "attribute": "lang"
              }
      }
    2. List of all criteria given below

      | Operator | Operator Property | Description | Example | | |------------------|---------------------|--------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------|---| | between | min, max, attribute | Check if the property mentioned in attribute has value between min and max | { "between" : { "min" : "10" , "max" : "20" , "attribute" : "discount" }} | | | lessThan | value, attribute | Check if the property mentioned in attribute has number less than value | { "lessThan" : { "value" : "80" , "attribute" : "price" } } | | | lessThanEqual | value, attribute | Check if the property mentioned in attribute has number less than or equal value | { "lessThanEqual" : { "value" : "80" , "attribute" : "price" } } | | | greaterThan | value, attribute | Check if the property mentioned in attribute has number greater than value | { "greaterThan" : { "value" : "80" , "attribute" : "price" } } | | | greaterThanEqual | value, attribute | Check if the property mentioned in attribute has number greater than or equal value | { "greaterThanEqual" : { "value" : "80" , "attribute" : "price" } } | | | equal | value, attribute | Check if the property mentioned in attribute has value equal to value provided | { "equal" : { "value" : "80" , "attribute" : "price" } } | | | contains | value, attribute | Check if the property mentioned in attribute contains the value given. | { "contains" : { "value" : "great" , "attribute" : "desc" }} | | | in | value, attribute | Check if the property mentioned in attribute is available in value array provided. | { "in" : { "value" : [ "C" , "Javascript" , "Python" ], "attribute" : "lang" }} | |

    3. A Rules JSON could be defined as

    { 
        "AND": [
            { "lessThan": {"value": "80", "attribute": "price"} },
            { "greaterThanEqual": {"value": "20", "attribute": "price"} },
            { "between": { "min": "10", "max": "20", "attribute": "discount" }}
        ]
    }
    {
        "OR":[
            { "contains": { "value": "great", "attribute": "desc" }},
            { "contains": { "value": "works", "attribute": "features"}},
            { "in": { "value": ["C","Javascript","Python"], "attribute": "lang"}},
        ]
    }
  4. Rules can be recursively nested as shown below

    {
        "AND": [
                { 
                    "AND": [
                        { "lessThan": {"value": "80", "attribute": "price"} },
                        { "greaterThanEqual": {"value": "20", "attribute": "price"} },
                        { "between": { "min": "10", "max": "20", "attribute": "discount" }}
                    ]
                },
                {
                    "OR":[
                        { "contains": { "value": "great", "attribute": "desc" }},
                        { "contains": { "value": "works", "attribute": "features"}},
                        { "in": { "value": ["C","Javascript","Python"], "attribute": "lang"}},
                    ]
                }
            ]
    }

Step2: Pass Data JSON and Rule JSON to Engine

    const Engine = require("@aipeel/json-rules-engine")

    Engine.apply(data,rule) // returns boolean - true or false

Complete end-to-end example below for reference

const Engine = require("@aipeel/json-rules-engine")

const rule = {
                    "AND": [
                            { 
                                "AND": [
                                    { "lessThan": {"value": "80", "attribute": "price"} },
                                    { "greaterThanEqual": {"value": "20", "attribute": "price"} },
                                    { "between": { "min": "10", "max": "20", "attribute": "discount" }}
                                ]
                            },
                            {
                                "OR":[
                                    { "contains": { "value": "great", "attribute": "desc" }},
                                    { "contains": { "value": "works", "attribute": "features"}},
                                    { "in": { "value": ["C","Javascript","Python"], "attribute": "lang"}},
                                ]
                            }
                        ]
                    }

let data = {
    id: 'ID1001',
    price: 50,
    discount: 10,
    desc: "This is a great product for developers",
    features: ["No Dependencies", "Open to Extension", "Synchronous", "works"],
    tags: ["json", "rule", "engine"],
    lang: "Javascript"
}

// example 1
console.log(`Does the data conform to rule? --> ${Engine.apply(data,rule)?"Yes":"No"}`);

//example 2
data.price = 100;
console.log(`Does the data conform to rule? --> ${Engine.apply(data,rule)?"Yes":"No"}`);

//example 3
data.price = 50;
data.discount = 21;
console.log(`Does the data conform to rule? --> ${Engine.apply(data,rule)?"Yes":"No"}`);

Output

Does the data conform to rule? --> Yes
Does the data conform to rule? --> No
Does the data conform to rule? --> No