https://github.com/dial-once/node-rules-engine
A node.js module to check if an event array matches some specifications.
https://github.com/dial-once/node-rules-engine
javascript nodejs rules
Last synced: about 2 months ago
JSON representation
A node.js module to check if an event array matches some specifications.
- Host: GitHub
- URL: https://github.com/dial-once/node-rules-engine
- Owner: dial-once
- License: gpl-2.0
- Created: 2015-09-15T15:36:02.000Z (over 9 years ago)
- Default Branch: develop
- Last Pushed: 2016-11-04T14:59:11.000Z (over 8 years ago)
- Last Synced: 2025-03-21T17:49:30.466Z (2 months ago)
- Topics: javascript, nodejs, rules
- Language: JavaScript
- Homepage:
- Size: 43.9 KB
- Stars: 13
- Watchers: 9
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# node-rules-engine
[](https://david-dm.org/dial-once/node-rules-engine)
[](https://www.codacy.com/app/pukoren/node-rules-engine)
[](https://codeclimate.com/github/dial-once/node-rules-engine)
[](https://codeclimate.com/github/dial-once/node-rules-engine)
[](https://codeship.com/projects/103052)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
```
```js
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)
```jsvar 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:
```js
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:
```js
var rules = [
{'page': {val: [1, 2, 3], should: true}},
{'click': {val: true, should: true}}
];
```User viewed any page:
```js
var rules = [
{'page': {val: '*', should: true}}
];
```
or
```js
var rules = [
{'page': {val: /.+/, should: true}}
];
```User viewed any page BUT page 3:
```js
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:
```js
{$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 JSUser should have made more than 10 clicks:
```js
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:
```js
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)
```js
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 }}
];
```