Skip to content

Commit f6dd06c

Browse files
Mithun SatheeshMithun Satheesh
authored andcommitted
[NodeRules] 6.0.0
- Process.tick polyfill for browsers - Unit Tests for Parallelism - Browser Support Definision in place - Renamed webpack Library Definition to "NodeRules" - Reduction in whole library size by 80%
1 parent f66c8b3 commit f6dd06c

File tree

6 files changed

+124
-13
lines changed

6 files changed

+124
-13
lines changed

.babelrc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
{
2-
"presets": ["@babel/preset-env"]
2+
"presets": [
3+
[
4+
"@babel/preset-env",
5+
{
6+
"targets": {
7+
"edge": "17",
8+
"firefox": "60",
9+
"chrome": "67",
10+
"safari": "11.1",
11+
},
12+
"useBuiltIns": "usage",
13+
}
14+
]
15+
]
316
}

dist/node-rules.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/node-rules.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
this.ignoreFactChanges = options.ignoreFactChanges;
1515
}
1616
return this;
17-
}
17+
};
1818
RuleEngine.prototype.init = function(rules) {
1919
this.rules = [];
2020
this.activeRules = [];
@@ -62,12 +62,12 @@
6262
if (outcome) {
6363
var _consequence = _rules[x].consequence;
6464
_consequence.ruleRef = _rules[x].id || _rules[x].name || 'index_'+x;
65-
process.nextTick(function() {
65+
thisHolder.nextTick(function() {
6666
matchPath.push(_consequence.ruleRef);
6767
_consequence.call(session, API, session);
6868
});
6969
} else {
70-
process.nextTick(function() {
70+
thisHolder.nextTick(function() {
7171
API.next();
7272
});
7373
}
@@ -82,11 +82,11 @@
8282
"next": function() {
8383
if (!ignoreFactChanges && !isEqual(lastSession, session)) {
8484
lastSession = clonedeep(session);
85-
process.nextTick(function() {
85+
thisHolder.nextTick(function() {
8686
API.restart();
8787
});
8888
} else {
89-
process.nextTick(function() {
89+
thisHolder.nextTick(function() {
9090
return FnRuleLoop(x + 1);
9191
});
9292
}
@@ -97,13 +97,20 @@
9797
var _rule = _rules[x].condition;
9898
_rule.call(session, API, session);
9999
} else {
100-
process.nextTick(function() {
100+
thisHolder.nextTick(function() {
101101
session.matchPath = matchPath;
102102
return callback(session);
103103
});
104104
}
105105
})(0);
106106
};
107+
RuleEngine.prototype.nextTick = function(callbackFn) {
108+
if (process && process.nextTick) {
109+
process.nextTick(callbackFn);
110+
} else {
111+
setTimeout(callbackFn, 0);
112+
}
113+
};
107114
RuleEngine.prototype.findRules = function(query) {
108115
if (typeof(query) === "undefined") {
109116
return this.rules;
@@ -117,22 +124,22 @@
117124
});
118125
});
119126
}
120-
}
127+
};
121128
RuleEngine.prototype.turn = function(state, filter) {
122129
var state = (state === "on" || state === "ON") ? true : false;
123130
var rules = this.findRules(filter);
124131
for (var i = 0, j = rules.length; i < j; i++) {
125132
rules[i].on = state;
126133
}
127134
this.sync();
128-
}
135+
};
129136
RuleEngine.prototype.prioritize = function(priority, filter) {
130137
priority = parseInt(priority, 10);
131138
var rules = this.findRules(filter);
132139
for (var i = 0, j = rules.length; i < j; i++) {
133140
rules[i].priority = priority;
134141
}
135142
this.sync();
136-
}
143+
};
137144
module.exports = RuleEngine;
138145
}(module.exports));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-rules",
3-
"version": "5.2.0",
3+
"version": "6.0.0",
44
"description": "Business Rules Engine for JavaScript",
55
"keywords": [
66
"bre",

test/index.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,95 @@ describe("Rules", function() {
408408
});
409409
});
410410
});
411+
describe("test Parallelism", function() {
412+
var rules = [
413+
{
414+
"name": "high credibility customer - avoid checks and bypass",
415+
"priority": 4,
416+
"on": true,
417+
"condition": function(R) {
418+
console.log(`Executing rule 1 on ${this.name}`);
419+
R.when(this.userCredibility && this.userCredibility > 5);
420+
},
421+
"consequence": function(R) {
422+
console.log(`Rule 1 matched for ${this.name} user credibility is more, then avoid further check. Accepting payment.`);
423+
this.result = true;
424+
R.stop();
425+
}
426+
},
427+
{
428+
"name": "block guest payment above 10000",
429+
"priority": 3,
430+
"condition": function(R) {
431+
console.log(`Executing rule 2 on ${this.name}`);
432+
R.when(this.customerType && this.transactionTotal > 10000 && this.customerType == "guest");
433+
},
434+
"consequence": function(R) {
435+
console.log(`Rule 2 matched for ${this.name} reject if above 10000 and customer type is guest. Rejecting payment.`);
436+
this.result = false;
437+
R.stop();
438+
}
439+
},
440+
{
441+
"name": "is customer guest?",
442+
"priority": 2,
443+
"condition": function(R) {
444+
console.log(`Executing rule 3 on ${this.name}`);
445+
R.when(!this.userLoggedIn);
446+
},
447+
"consequence": function(R) {
448+
console.log(`Rule 3 matched for ${this.name} support rule written for blocking payment above 10000 from guests.`);
449+
console.log("Process left to chain with rule 2.");
450+
this.customerType = "guest";
451+
// the fact has been altered above, so all rules will run again since ignoreFactChanges is not set.
452+
R.next();
453+
}
454+
},
455+
{
456+
"name": "block Cashcard Payment",
457+
"priority": 1,
458+
"condition": function(R) {
459+
console.log(`Executing rule 4 on ${this.name}`);
460+
R.when(this.cardType == "Cash Card");
461+
},
462+
"consequence": function(R) {
463+
console.log(`Rule 4 matched for ${this.name} reject the payment if cash card. Rejecting payment.`);
464+
this.result = false;
465+
R.stop();
466+
}
467+
}
468+
];
469+
470+
var straightFact = {
471+
"name": "straightFact",
472+
"userCredibility": 1,
473+
"userLoggedIn": true,
474+
"transactionTotal": 12000,
475+
"cardType": "Cash Card"
476+
};
477+
478+
/** example of a chaned up rule. will take two iterations. ****/
479+
var chainedFact = {
480+
"name": "chainedFact",
481+
"userCredibility": 2,
482+
"userLoggedIn": false,
483+
"transactionTotal": 100000,
484+
"cardType": "Credit Card"
485+
};
486+
487+
it("context switches and finishes the fact which needs least iteration first", function(done) {
488+
var R = new RuleEngine(rules);
489+
var isStraightFactFast = false;
490+
491+
R.execute(chainedFact, function(result) {
492+
expect(isStraightFactFast).eql(true);
493+
done();
494+
});
495+
496+
R.execute(straightFact, function(result) {
497+
isStraightFactFast = true;
498+
});
499+
500+
});
501+
});
411502
});

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
path: path.resolve(__dirname, 'dist'),
1010
filename: 'node-rules.min.js',
1111
libraryTarget: 'umd',
12-
library: 'node-rules'
12+
library: 'NodeRules'
1313
},
1414
module: {
1515
rules: [

0 commit comments

Comments
 (0)