Skip to content

Commit

Permalink
Fix security vulnerabilities for dependencies
Browse files Browse the repository at this point in the history
- Upgraded to safer versions
- Cleaned up code
- Removed unwanted dependencies
- Moved to Jest for testing
  • Loading branch information
mithunsatheesh committed Jul 1, 2021
1 parent b92d849 commit 4451ff2
Show file tree
Hide file tree
Showing 19 changed files with 5,956 additions and 4,556 deletions.
5 changes: 3 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
"chrome": "67",
"safari": "11.1"
},
"useBuiltIns": "usage"
"useBuiltIns": "usage",
"corejs": 3
}
]
]
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/node_modules
/node_modules/*
/coverage
Expand All @@ -8,3 +7,5 @@
npm-debug.log
.DS_Store
.coveralls.yml
.nyc_output
.nyc_output/*
60 changes: 25 additions & 35 deletions test/index.js → __tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var RuleEngine = require('../index');
var expect = require("chai").expect;
describe("Rules", function() {
describe(".init()", function() {
it("should empty the existing rule array", function() {
Expand All @@ -13,7 +12,7 @@ describe("Rules", function() {
}];
var R = new RuleEngine(rules);
R.init();
expect(R.rules).to.eql([]);
expect(R.rules).toEqual([]);
});
});
describe(".register()", function() {
Expand All @@ -27,7 +26,7 @@ describe("Rules", function() {
}
}];
var R = new RuleEngine(rules);
expect(R.rules[0].on).to.eql(true);
expect(R.rules[0].on).toEqual(true);
});
it("Rule can be passed to register as both arrays and individual objects", function() {
var rule = {
Expand All @@ -40,7 +39,7 @@ describe("Rules", function() {
};
var R1 = new RuleEngine(rule);
var R2 = new RuleEngine([rule]);
expect(R1.rules).to.eql(R2.rules);
expect(R1.rules).toEqual(R2.rules);
});
it("Rules can be appended multiple times via register after creating rule engine instance", function() {
var rules = [{
Expand All @@ -62,9 +61,9 @@ describe("Rules", function() {
var R2 = new RuleEngine(rules[0]);
var R3 = new RuleEngine();
R2.register(rules[1]);
expect(R1.rules).to.eql(R2.rules);
expect(R1.rules).toEqual(R2.rules);
R3.register(rules);
expect(R1.rules).to.eql(R3.rules);
expect(R1.rules).toEqual(R3.rules);
});
});
describe(".sync()", function() {
Expand All @@ -90,7 +89,7 @@ describe("Rules", function() {
}];
var R = new RuleEngine();
R.register(rules);
expect(R.activeRules).not.to.eql(R.rules);
expect(R.activeRules).not.toEqual(R.rules);
});
it("should sort the rules accroding to priority, if priority is present", function() {
var rules = [{
Expand Down Expand Up @@ -123,7 +122,7 @@ describe("Rules", function() {
}];
var R = new RuleEngine();
R.register(rules);
expect(R.activeRules[2].index).to.eql(2);
expect(R.activeRules[2].index).toEqual(2);
});
});
describe(".exec()", function() {
Expand All @@ -141,7 +140,7 @@ describe("Rules", function() {
R.execute({
"transactionTotal": 200
}, function(result) {
expect(result.result).to.eql(false);
expect(result.result).toEqual(false);
});
});
it("should chain rules and find result with next()", function() {
Expand All @@ -168,7 +167,7 @@ describe("Rules", function() {
"transactionTotal": 200,
"card": "VISA"
}, function(result) {
expect(result.result).to.eql("Custom Result");
expect(result.result).toEqual("Custom Result");
});
});
it("should provide access to rule definition properties via rule()", function() {
Expand All @@ -189,8 +188,8 @@ describe("Rules", function() {
R.execute({
"input": true
}, function(result) {
expect(result.ruleName).to.eql(rule.name);
expect(result.ruleID).to.eql(rule.id);
expect(result.ruleName).toEqual(rule.name);
expect(result.ruleID).toEqual(rule.id);
});
});
it("should include the matched rule path", function() {
Expand Down Expand Up @@ -246,7 +245,7 @@ describe("Rules", function() {
"x": true,
"y": false
}, function(result) {
expect(result.matchPath).to.eql([rules[0].name, rules[2].id, lastMatch]);
expect(result.matchPath).toEqual([rules[0].name, rules[2].id, lastMatch]);
});
});

Expand All @@ -264,7 +263,7 @@ describe("Rules", function() {
R.execute({
"transactionTotal": 200
}, function(result) {
expect(result.result).to.eql(false);
expect(result.result).toEqual(false);
});
});

Expand All @@ -284,7 +283,7 @@ describe("Rules", function() {
R.execute({
"transactionTotal": 200
}, function(result) {
expect(result.result).to.eql(false);
expect(result.result).toEqual(false);
});
});

Expand All @@ -311,21 +310,21 @@ describe("Rules", function() {
it("find selector function for rules should exact number of matches", function() {
expect(R.findRules({
"id": "one"
}).length).to.eql(1);
}).length).toEqual(1);
});
it("find selector function for rules should give the correct match as result", function() {
expect(R.findRules({
"id": "one"
})[0].id).to.eql("one");
})[0].id).toEqual("one");
});
it("find selector function should filter off undefined entries in the query if any", function() {
expect(R.findRules({
"id": "one",
"myMistake": undefined
})[0].id).to.eql("one");
})[0].id).toEqual("one");
});
it("find without condition works fine", function() {
expect(R.findRules().length).to.eql(2);
expect(R.findRules().length).toEqual(2);
});
});
describe(".turn()", function() {
Expand Down Expand Up @@ -354,15 +353,15 @@ describe("Rules", function() {
});
expect(R.findRules({
"id": "one"
})[0].on).to.eql(false);
})[0].on).toEqual(false);
});
it("checking whether turn on rules work as expected", function() {
R.turn("ON", {
"id": "two"
});
expect(R.findRules({
"id": "two"
})[0].on).to.eql(true);
})[0].on).toEqual(true);
});
});
describe(".prioritize()", function() {
Expand Down Expand Up @@ -401,13 +400,13 @@ describe("Rules", function() {
});
expect(R.findRules({
"id": "one"
})[0].priority).to.eql(10);
})[0].priority).toEqual(10);
});
it("checking whether rules reorder after prioritize", function() {
R.prioritize(10, {
"id": "one"
});
expect(R.activeRules[0].id).to.eql("one");
expect(R.activeRules[0].id).toEqual("one");
});
});
describe("ignoreFactChanges", function() {
Expand All @@ -432,7 +431,7 @@ describe("Rules", function() {
var R = new RuleEngine(rules, { ignoreFactChanges: true });

R.execute(fact, function(result) {
expect(result.errors).to.have.length(1);
expect(result.errors).toHaveLength(1);
done();
});
});
Expand All @@ -444,11 +443,9 @@ describe("Rules", function() {
"priority": 4,
"on": true,
"condition": function(R) {
console.log(`Executing rule 1 on ${this.name}`);
R.when(this.userCredibility && this.userCredibility > 5);
},
"consequence": function(R) {
console.log(`Rule 1 matched for ${this.name} user credibility is more, then avoid further check. Accepting payment.`);
this.result = true;
R.stop();
}
Expand All @@ -457,11 +454,9 @@ describe("Rules", function() {
"name": "block guest payment above 10000",
"priority": 3,
"condition": function(R) {
console.log(`Executing rule 2 on ${this.name}`);
R.when(this.customerType && this.transactionTotal > 10000 && this.customerType == "guest");
},
"consequence": function(R) {
console.log(`Rule 2 matched for ${this.name} reject if above 10000 and customer type is guest. Rejecting payment.`);
this.result = false;
R.stop();
}
Expand All @@ -470,12 +465,9 @@ describe("Rules", function() {
"name": "is customer guest?",
"priority": 2,
"condition": function(R) {
console.log(`Executing rule 3 on ${this.name}`);
R.when(!this.userLoggedIn);
},
"consequence": function(R) {
console.log(`Rule 3 matched for ${this.name} support rule written for blocking payment above 10000 from guests.`);
console.log("Process left to chain with rule 2.");
this.customerType = "guest";
// the fact has been altered above, so all rules will run again since ignoreFactChanges is not set.
R.next();
Expand All @@ -485,11 +477,9 @@ describe("Rules", function() {
"name": "block Cashcard Payment",
"priority": 1,
"condition": function(R) {
console.log(`Executing rule 4 on ${this.name}`);
R.when(this.cardType == "Cash Card");
},
"consequence": function(R) {
console.log(`Rule 4 matched for ${this.name} reject the payment if cash card. Rejecting payment.`);
this.result = false;
R.stop();
}
Expand Down Expand Up @@ -518,7 +508,7 @@ describe("Rules", function() {
var isStraightFactFast = false;

R.execute(chainedFact, function(result) {
expect(isStraightFactFast).eql(true);
expect(isStraightFactFast).toBe(true);
done();
});

Expand All @@ -528,4 +518,4 @@ describe("Rules", function() {

});
});
});
});
Loading

0 comments on commit 4451ff2

Please sign in to comment.