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-engine

v0.3.0

Published

A node.js module to check if an event array matches some specifications. Usefull when you want to compare many rules with many events and check if the final output is true/false.

Downloads

50

Readme

node-rules-engine

David Codacy Code Climate Code Climate Codeship

A node.js module to check if an event array matches some specifications. Usefull when you want to compare many rules with many events and check if the final output is true/false.

Dial Once uses this module to easily check if a user match a goal/funnel using our interfaces. See exemple below for a more explanatory exemple.

installation

npm install rules-engine
var engine = require('rules-engine');
engine.apply(events, rules).then(function(result){
  console.log(result); //boolean, true if events are matching rules, false otherwise
});

example

We want here to check if user viewed page 1, page 3, and then made a click (basic conversion test). ('page' and 'click' here are arbitrary and you can use whatever you want)


var engine = require('rules-engine');

var events = [
  {'page': {val: 1}},
  {'page': {val: 3}},
  {'click': {val: true}}
];

var rules = [
  {'page': {val: 1, should: true}},
  {'page': {val: 3, should: true}},
  {'click': {val: true, should: true}}
];

engine.apply(events, rules).then(function(result){
  console.log(result); //true!
});

Then things can be a bit more complicated, if we want to check if user viewed page 1, 3 but did not make any click:

var rules = [
  {'page': {val: 1, should: true}},
  {'page': {val: 3, should: true}},
  {'click': {val: true, should: false}}
];
engine.apply(events, rules).then(function(result){
  console.log(result); //false!
});

Some more cases can be handled, like 'user viewed either page 1, 2, or 3 and made a click:

var rules = [
  {'page': {val: [1, 2, 3], should: true}},
  {'click': {val: true, should: true}}
];

User viewed any page:

var rules = [
  {'page': {val: '*', should: true}}
];

or

var rules = [
  {'page': {val: /.+/, should: true}}
];

User viewed any page BUT page 3:

var rules = [
  {'page': {val: '*', should: true}},
  {'page': {val: 3, should: false}}
];

complex rules exemple

The following rules can be used in val to compute complex rules:

{$gt: 0} //true if event val is greater than the value
{$lt: 0} //true if event val is lesser than the value
{$gte: 0} //greater than or equal
{$lte: 0} //lesser than or equal

Value can be a Date, number, string, etc. Anything natively comparable in JS

User should have made more than 10 clicks:

var events = [
 {'clicks': {val: 12}}
];
var rules = [
  {'clicks': {val: {$gt: 10}, should: true}}
];

OR conditions and optional rules

You can specify some rules as optional, thus if they don't match, other rules will have priority.

User should do more than 10 clicks OR less than 3 pageviews:

var events = [
 {'clicks': {val: 12}}
];
var rules = [
  {'clicks': {val: {$gt: 10}, should: true, optional: true}},
  {'views': {val: {$lt: 3}, should: true, optional: true}}
];

If none are matched, it will be rejected. If one of the two matches, its a true!

You can then make some complex scenario, like: User should do more than 10 clicks, AND (views less than 3 pages OR make 1 comment)

var events = [
 {'clicks': {val: 12}}
];
var rules = [
  {'clicks': {val: {$gt: 10}, should: true}},
  {'views': {val: {$lt: 3}, should: true, optional: true }},
  {'comments': {val: 1, should: true, optional: true }}
];