Skip to content

Commit 1665821

Browse files
Evals moved to external loader as before. internal functions are injected in function parameters without deending on eval to induce dynamic scopes
1 parent 33a19db commit 1665821

File tree

1 file changed

+40
-44
lines changed

1 file changed

+40
-44
lines changed

test/rules.js

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@ var rules = [
3535
"priority": 3,
3636
"on":1,
3737
"condition":
38-
function(cb) {
39-
cb(this && (this.transactionTotal < 500));
38+
function(R) {
39+
R.when(this && (this.transactionTotal < 500));
4040
},
4141
"consequence":
42-
function(cb) {
42+
function(R) {
4343
console.log("Rule 1 matched for "+this.name+": blocks transactions below value 500. Rejecting payment.");
4444
this.result = false;
45-
this.complete = true;
46-
cb();
45+
R.stop();
4746
}
4847
},
4948
/**** Rule 2 ****/
@@ -53,15 +52,14 @@ var rules = [
5352
"priority":2,
5453
"on":1,
5554
"condition":
56-
function(cb) {
57-
cb(this && this.userCredibility && (this.userCredibility > 5));
55+
function(R) {
56+
R.when(this && this.userCredibility && (this.userCredibility > 5));
5857
},
5958
"consequence":
60-
function(cb) {
59+
function(R) {
6160
console.log("Rule 2 matched for "+this.name+": if the users credibility value is more, then avoid checking further. Accepting payment. ");
6261
this.result = true;
63-
this.complete = true;
64-
cb();
62+
R.stop();
6563
}
6664
},
6765
/**** Rule 3 ****/
@@ -71,15 +69,14 @@ var rules = [
7169
"priority": 4,
7270
"on":1,
7371
"condition":
74-
function(cb) {
75-
cb(this && (this.cardType == "Credit Card") && (this.cardIssuer == "American Express") && (this.transactionTotal > 1000));
72+
function(R) {
73+
R.when(this && (this.cardType == "Credit Card") && (this.cardIssuer == "American Express") && (this.transactionTotal > 1000));
7674
},
7775
"consequence":
78-
function(cb) {
76+
function(R) {
7977
console.log("Rule 3 matched for "+this.name+": filter American Express credit cards for payment above 10000. Rejecting payment.");
8078
this.result = false;
81-
this.complete = true;
82-
cb();
79+
R.stop();
8380
}
8481
},
8582
/**** Rule 4 ****/
@@ -89,18 +86,17 @@ var rules = [
8986
"priority":8,
9087
"on":1,
9188
"condition":
92-
function(cb) {
89+
function(R) {
9390

94-
cb(this && (this.cardType == "Cash Card"));
91+
R.when(this && (this.cardType == "Cash Card"));
9592

9693
},
9794
"consequence":
98-
function(cb) {
95+
function(R) {
9996

10097
console.log("Rule 4 matched for "+this.name+": reject the payment if the payment type belong to cash card. Rejecting payment.");
10198
this.result = false;
102-
this.complete = true;
103-
cb();
99+
R.stop();
104100

105101
}
106102
},
@@ -111,15 +107,14 @@ var rules = [
111107
"priority":6,
112108
"on":1,
113109
"condition":
114-
function(cb) {
115-
cb(this && this.customerType && (this.transactionTotal > 10000) && (this.customerType == "guest"));
110+
function(R) {
111+
R.when(this && this.customerType && (this.transactionTotal > 10000) && (this.customerType == "guest"));
116112
},
117113
"consequence":
118-
function(cb) {
114+
function(R) {
119115
console.log("Rule 5 matched for "+this.name+": reject the payment if the payment above 10000 and customer type is guest. Rejecting payment.");
120116
this.result = false;
121-
this.complete = true;
122-
cb();
117+
R.stop();
123118
}
124119
},
125120
/**** Rule 6 ****/
@@ -129,14 +124,14 @@ var rules = [
129124
"priority":7,
130125
"on":1,
131126
"condition":
132-
function(cb) {
133-
cb(this && !this.userLoggedIn);
127+
function(R) {
128+
R.when(this && !this.userLoggedIn);
134129
},
135130
"consequence":
136-
function(cb) {
131+
function(R) {
137132
console.log("Rule 6 matched for "+this.name+": support rule written for blocking payment above 10000 from guests. complete left to chain with rule 6.");
138133
this.customerType = "guest";
139-
cb();
134+
R.next();
140135
}
141136
},
142137
/**** Rule 7 ****/
@@ -146,15 +141,14 @@ var rules = [
146141
"priority":5,
147142
"on":1,
148143
"condition":
149-
function(cb) {
150-
cb(this && this.appCode && (this.appCode == "MOBI4"));
144+
function(R) {
145+
R.when(this && this.appCode && (this.appCode == "MOBI4"));
151146
},
152147
"consequence":
153-
function(cb) {
148+
function(R) {
154149
console.log("Rule 7 matched for "+this.name+": turn on this rule to block the payment from a specific app. Reject Paymant.");
155150
this.result = false;
156-
this.complete = true;
157-
cb();
151+
R.stop();
158152
}
159153
},
160154
/**** Rule 8 ****/
@@ -164,15 +158,14 @@ var rules = [
164158
"priority":2,
165159
"on":1,
166160
"condition":
167-
function(cb) {
168-
cb(this && this.eventRiskFactor && (this.eventRiskFactor < 5));
161+
function(R) {
162+
R.when(this && this.eventRiskFactor && (this.eventRiskFactor < 5));
169163
},
170164
"consequence":
171-
function(cb) {
165+
function(R) {
172166
console.log("Rule 8 matched for "+this.name+": if the event is top priority event, then do further checks else leave. Accept payment as low priority event.");
173167
this.result = true;
174-
this.complete = true;
175-
cb();
168+
R.stop();
176169
}
177170
},
178171
/**** Rule 9 ****/
@@ -182,7 +175,7 @@ var rules = [
182175
"priority":3,
183176
"on":1,
184177
"condition":
185-
function(cb) {
178+
function(R) {
186179
var allowedRegexp = new RegExp('^(?:' +
187180
[
188181
"10.X.X.X",
@@ -193,17 +186,20 @@ var rules = [
193186
"74.23.211.92"
194187
].join('|').replace(/\./g, '\\.').replace(/X/g, '[^.]+') +
195188
')$');
196-
cb(this && this.userIP && this.userIP.match(allowedRegexp));
189+
R.when(this && this.userIP && this.userIP.match(allowedRegexp));
197190
},
198191
"consequence":
199-
function(cb) {
192+
function(R) {
200193
console.log("Rule 9 matched for "+this.name+": if the ip fall in the given list of formats, then block the transaction. Rejecting payment.");
201194
this.result = false;
202-
this.complete = true;
203-
cb();
195+
R.stop();
204196
}
205197
},
206198
/**** Rule 10 * fails
199+
200+
This rule has to be rewritten. Because the rules defined should not have closures which prevent its
201+
export-ability. Pass the details as fact.
202+
207203
{
208204
"name" : "check if user's name is blacklisted in db",
209205
"description" : "if the user's name is found then block transaction.",

0 commit comments

Comments
 (0)