@@ -71,8 +71,15 @@ class Rule extends EventEmitter {
71
71
* @param {object } conditions - conditions, root element must be a boolean operator
72
72
*/
73
73
setConditions ( conditions ) {
74
- if ( ! Object . prototype . hasOwnProperty . call ( conditions , 'all' ) && ! Object . prototype . hasOwnProperty . call ( conditions , 'any' ) && ! Object . prototype . hasOwnProperty . call ( conditions , 'not' ) && ! Object . prototype . hasOwnProperty . call ( conditions , 'condition' ) ) {
75
- throw new Error ( '"conditions" root must contain a single instance of "all", "any", "not", or "condition"' )
74
+ if (
75
+ ! Object . prototype . hasOwnProperty . call ( conditions , 'all' ) &&
76
+ ! Object . prototype . hasOwnProperty . call ( conditions , 'any' ) &&
77
+ ! Object . prototype . hasOwnProperty . call ( conditions , 'not' ) &&
78
+ ! Object . prototype . hasOwnProperty . call ( conditions , 'condition' )
79
+ ) {
80
+ throw new Error (
81
+ '"conditions" root must contain a single instance of "all", "any", "not", or "condition"'
82
+ )
76
83
}
77
84
this . conditions = new Condition ( conditions )
78
85
return this
@@ -86,7 +93,11 @@ class Rule extends EventEmitter {
86
93
*/
87
94
setEvent ( event ) {
88
95
if ( ! event ) throw new Error ( 'Rule: setEvent() requires event object' )
89
- if ( ! Object . prototype . hasOwnProperty . call ( event , 'type' ) ) throw new Error ( 'Rule: setEvent() requires event object with "type" property' )
96
+ if ( ! Object . prototype . hasOwnProperty . call ( event , 'type' ) ) {
97
+ throw new Error (
98
+ 'Rule: setEvent() requires event object with "type" property'
99
+ )
100
+ }
90
101
this . ruleEvent = {
91
102
type : event . type
92
103
}
@@ -170,9 +181,11 @@ class Rule extends EventEmitter {
170
181
sets [ priority ] . push ( condition )
171
182
return sets
172
183
} , { } )
173
- return Object . keys ( factSets ) . sort ( ( a , b ) => {
174
- return Number ( a ) > Number ( b ) ? - 1 : 1 // order highest priority -> lowest
175
- } ) . map ( ( priority ) => factSets [ priority ] )
184
+ return Object . keys ( factSets )
185
+ . sort ( ( a , b ) => {
186
+ return Number ( a ) > Number ( b ) ? - 1 : 1 // order highest priority -> lowest
187
+ } )
188
+ . map ( ( priority ) => factSets [ priority ] )
176
189
}
177
190
178
191
/**
@@ -181,7 +194,12 @@ class Rule extends EventEmitter {
181
194
* @return {Promise(RuleResult) } rule evaluation result
182
195
*/
183
196
evaluate ( almanac ) {
184
- const ruleResult = new RuleResult ( this . conditions , this . ruleEvent , this . priority , this . name )
197
+ const ruleResult = new RuleResult (
198
+ this . conditions ,
199
+ this . ruleEvent ,
200
+ this . priority ,
201
+ this . name
202
+ )
185
203
186
204
/**
187
205
* Evaluates the rule conditions
@@ -190,7 +208,7 @@ class Rule extends EventEmitter {
190
208
*/
191
209
const evaluateCondition = ( condition ) => {
192
210
if ( condition . isConditionReference ( ) ) {
193
- return realize ( this . engine . conditions . get ( condition . condition ) , condition )
211
+ return realize ( condition )
194
212
} else if ( condition . isBooleanOperator ( ) ) {
195
213
const subConditions = condition [ condition . operator ]
196
214
let comparisonPromise
@@ -202,14 +220,15 @@ class Rule extends EventEmitter {
202
220
comparisonPromise = not ( subConditions )
203
221
}
204
222
// for booleans, rule passing is determined by the all/any/not result
205
- return comparisonPromise . then ( comparisonValue => {
223
+ return comparisonPromise . then ( ( comparisonValue ) => {
206
224
const passes = comparisonValue === true
207
225
condition . result = passes
208
226
return passes
209
227
} )
210
228
} else {
211
- return condition . evaluate ( almanac , this . engine . operators )
212
- . then ( evaluationResult => {
229
+ return condition
230
+ . evaluate ( almanac , this . engine . operators )
231
+ . then ( ( evaluationResult ) => {
213
232
const passes = evaluationResult . result
214
233
condition . factResult = evaluationResult . leftHandSideValue
215
234
condition . result = passes
@@ -225,13 +244,14 @@ class Rule extends EventEmitter {
225
244
* @return {Promise(boolean) } whether conditions evaluated truthy or falsey based on condition evaluation + method
226
245
*/
227
246
const evaluateConditions = ( conditions , method ) => {
228
- if ( ! ( Array . isArray ( conditions ) ) ) conditions = [ conditions ]
247
+ if ( ! Array . isArray ( conditions ) ) conditions = [ conditions ]
229
248
230
- return Promise . all ( conditions . map ( ( condition ) => evaluateCondition ( condition ) ) )
231
- . then ( conditionResults => {
232
- debug ( 'rule::evaluateConditions results' , conditionResults )
233
- return method . call ( conditionResults , ( result ) => result === true )
234
- } )
249
+ return Promise . all (
250
+ conditions . map ( ( condition ) => evaluateCondition ( condition ) )
251
+ ) . then ( ( conditionResults ) => {
252
+ debug ( 'rule::evaluateConditions results' , conditionResults )
253
+ return method . call ( conditionResults , ( result ) => result === true )
254
+ } )
235
255
}
236
256
237
257
/**
@@ -267,14 +287,18 @@ class Rule extends EventEmitter {
267
287
cursor = cursor . then ( ( setResult ) => {
268
288
// after the first set succeeds, don't fire off the remaining promises
269
289
if ( ( operator === 'any' && setResult === true ) || stop ) {
270
- debug ( 'prioritizeAndRun::detected truthy result; skipping remaining conditions' )
290
+ debug (
291
+ 'prioritizeAndRun::detected truthy result; skipping remaining conditions'
292
+ )
271
293
stop = true
272
294
return true
273
295
}
274
296
275
297
// after the first set fails, don't fire off the remaining promises
276
298
if ( ( operator === 'all' && setResult === false ) || stop ) {
277
- debug ( 'prioritizeAndRun::detected falsey result; skipping remaining conditions' )
299
+ debug (
300
+ 'prioritizeAndRun::detected falsey result; skipping remaining conditions'
301
+ )
278
302
stop = true
279
303
return false
280
304
}
@@ -309,17 +333,25 @@ class Rule extends EventEmitter {
309
333
* @return {Promise(boolean) } condition evaluation result
310
334
*/
311
335
const not = ( condition ) => {
312
- return prioritizeAndRun ( [ condition ] , 'not' ) . then ( result => ! result )
336
+ return prioritizeAndRun ( [ condition ] , 'not' ) . then ( ( result ) => ! result )
313
337
}
314
338
315
- const realize = ( condition , conditionReference ) => {
339
+ /**
340
+ * Dereferences the condition reference and then evaluates it.
341
+ * @param {Condition } conditionReference
342
+ * @returns {Promise(boolean) } condition evaluation result
343
+ */
344
+ const realize = ( conditionReference ) => {
345
+ const condition = this . engine . conditions . get ( conditionReference . condition )
316
346
if ( ! condition ) {
317
347
if ( this . engine . allowUndefinedConditions ) {
318
348
// undefined conditions always fail
319
349
conditionReference . result = false
320
350
return Promise . resolve ( false )
321
351
} else {
322
- throw new Error ( `No condition ${ conditionReference . condition } exists` )
352
+ throw new Error (
353
+ `No condition ${ conditionReference . condition } exists`
354
+ )
323
355
}
324
356
} else {
325
357
// project the referenced condition onto reference object and evaluate it.
@@ -336,18 +368,27 @@ class Rule extends EventEmitter {
336
368
const processResult = ( result ) => {
337
369
ruleResult . setResult ( result )
338
370
const event = result ? 'success' : 'failure'
339
- return this . emitAsync ( event , ruleResult . event , almanac , ruleResult ) . then ( ( ) => ruleResult )
371
+ return this . emitAsync ( event , ruleResult . event , almanac , ruleResult ) . then (
372
+ ( ) => ruleResult
373
+ )
340
374
}
341
375
342
376
if ( ruleResult . conditions . any ) {
343
- return any ( ruleResult . conditions . any )
344
- . then ( result => processResult ( result ) )
377
+ return any ( ruleResult . conditions . any ) . then ( ( result ) =>
378
+ processResult ( result )
379
+ )
345
380
} else if ( ruleResult . conditions . all ) {
346
- return all ( ruleResult . conditions . all )
347
- . then ( result => processResult ( result ) )
381
+ return all ( ruleResult . conditions . all ) . then ( ( result ) =>
382
+ processResult ( result )
383
+ )
384
+ } else if ( ruleResult . conditions . not ) {
385
+ return not ( ruleResult . conditions . not ) . then ( ( result ) =>
386
+ processResult ( result )
387
+ )
348
388
} else {
349
- return not ( ruleResult . conditions . not )
350
- . then ( result => processResult ( result ) )
389
+ return realize (
390
+ ruleResult . conditions
391
+ ) . then ( ( result ) => processResult ( result ) )
351
392
}
352
393
}
353
394
}
0 commit comments