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

rules-runner

v1.2.1

Published

A JS business rules engine for node

Downloads

812

Readme

travis build status

Introduction

rules-runner allows you to cleanly abstract your rules away from your application code

  • run your dataset against a config JSON object
  • results can modify your dataset or can return a new dataset of outcomes
  • It's isomorphic and has minimal package dependencies - great for the browser and the server

Installation

npm install rules-runner

Examples

var Rules = require("rules-runner");

var config = {
  "Must be 16 or older if no adult is present": {
    //if ALL "tests" in the if statement match,
    if: {
      "person.age": {
        lessThan: 16
      },
      "person.adultPresent": false
    },
    //process all of the outcomes
    then: {
      "person.error": "Must be 16 or older if no adult is present",
      "errors.all[]": "person"
    }
  },
  "Must be employed": {
    if: {
      "company.isEmployed": false
    },
    then: {
      "company.error": "Must be employed",
      "errors.all[]": "company" //add [] to the end of a key to push values onto an array
    }
  };
};

var data = {
  person: {
    age: 15,
    adultPresent: false
  },
  company: {
    isEmployed: false
  }
};

var rules = new Rules(config);
rules.run(data);
assert.equal(data.person.error, "Must be 16 or older if no adult is present");
assert.equal(data.company.error, "Must be employed");
assert.deepEqual(data.errors.all, ["person", "company"]);
//by default, rules modify data object
//only want results leaving data unchanged? 
//var results = rules.run(data, {rulesModifyData: false});

##Use an array of if statements to treat conditions as else if or OR like


var config = {
  "Person will be in house if person is tired or hungry": {
    if: [
      {"person.tired": true}, //if this matches
      {"person.hungry": true} //OR if this matches
    ],
    then: {
      "person.location": "house" //then run this
    }
  }
};
    
var data = {
  person: {
    tired: false,
    hungry: true
  }
};

var rules = new Rules(config);
rules.run(data);

assert.equal(data.person.location, 'house');

##otherwise will process if no conditions match


var config = {
  "Person will be in house if person is tired or hungry": {
    if: [
      {"person.tired": true},
      {"person.hungry": true}
    ],
    then: {
      "person.location": "house"
    },
    otherwise: { // if all conditions are false
      "person.location": 'work'
    }
  }
};
var data = {
  person: {
    tired: false,
    hungry: false
  }
};

var rules = new Rules(config);
rules.run(data);

assert.equal(data.person.location, 'work');

Comparators/Tests

  • between: "person.age": {between: [1, 20]}
  • equality/scalar values: "person.exists": true "person.firstName": "John"
  • contains: `"person.name": {contains: "Jr"}`` (also checks for values in arrays)
  • greaterThan: "person.age": {greaterThan: 20}
  • in: "person.state": {in: ["CA", "TX", "NY"]}
  • lessThan: "person.age": {lessThan: 21}
  • matches: "person.name": {matches: "/(john|bob|mary)/i"i
  • not: "person.state": {not: "CA"}, "person.state": {not: {in: ["CA", "TX"]}}

Options

  • caseSensitive
    • default: true
    • contains and equals ignore case
  • rulesModifyData
    • default: true
    • matching rules modify original data set rules.run() returns modified dataset
    • when false, rules create a new object, which gets returned
  • strict
    • default: false
    • useful for debugging. when true, if a rule path (i.e. if: "person.age") isn't found in data, an error is thrown
    • when false, a rule path that isn't set in data evaluates to undefined
  • stringNumbers:
    • default: true
    • greaterThan, lessThan, and between comparators will parse numbers. in will match with == instead of ===