UNPKG

@splitsoftware/splitio-commons

Version:
22 lines (21 loc) 964 B
import { findIndex } from '../../utils/lang'; import { thenable } from '../../utils/promise/thenable'; import { ENGINE_COMBINER_AND } from '../../logger/constants'; export function andCombinerContext(log, matchers) { function andResults(results) { // Array.prototype.every is supported by target environments var hasMatchedAll = results.every(function (value) { return value; }); log.debug(ENGINE_COMBINER_AND, [hasMatchedAll]); return hasMatchedAll; } return function andCombiner(key, attributes, splitEvaluator) { var matcherResults = matchers.map(function (matcher) { return matcher(key, attributes, splitEvaluator); }); // If any matching result is a thenable we should use Promise.all if (findIndex(matcherResults, thenable) !== -1) { return Promise.all(matcherResults).then(andResults); } else { return andResults(matcherResults); } }; }