MCPcopy
hub / github.com/CacheControl/json-rules-engine / prioritizeAndRun

Method prioritizeAndRun

src/rule.js:267–310  ·  view source on GitHub ↗
(conditions, operator)

Source from the content-addressed store, hash-verified

265 * @return {Promise(boolean)} rule evaluation result
266 */
267 const prioritizeAndRun = (conditions, operator) => {
268 if (conditions.length === 0) {
269 return Promise.resolve(true)
270 }
271 if (conditions.length === 1) {
272 // no prioritizing is necessary, just evaluate the single condition
273 // 'all' and 'any' will give the same results with a single condition so no method is necessary
274 // this also covers the 'not' case which should only ever have a single condition
275 return evaluateCondition(conditions[0])
276 }
277 let method = Array.prototype.some
278 if (operator === 'all') {
279 method = Array.prototype.every
280 }
281 const orderedSets = this.prioritizeConditions(conditions)
282 let cursor = Promise.resolve()
283 // use for() loop over Array.forEach to support IE8 without polyfill
284 for (let i = 0; i < orderedSets.length; i++) {
285 const set = orderedSets[i]
286 let stop = false
287 cursor = cursor.then((setResult) => {
288 // after the first set succeeds, don't fire off the remaining promises
289 if ((operator === 'any' && setResult === true) || stop) {
290 debug(
291 'prioritizeAndRun::detected truthy result; skipping remaining conditions'
292 )
293 stop = true
294 return true
295 }
296
297 // after the first set fails, don't fire off the remaining promises
298 if ((operator === 'all' && setResult === false) || stop) {
299 debug(
300 'prioritizeAndRun::detected falsey result; skipping remaining conditions'
301 )
302 stop = true
303 return false
304 }
305 // all conditions passed; proceed with running next set in parallel
306 return evaluateConditions(set, method)
307 })
308 }
309 return cursor
310 }
311
312 /**
313 * Runs an 'any' boolean operator on an array of conditions

Callers

nothing calls this directly

Calls 2

prioritizeConditionsMethod · 0.95
debugFunction · 0.85

Tested by

no test coverage detected