@splitsoftware/splitio-commons
Version:
Split JavaScript SDK common components
53 lines (52 loc) • 2.7 kB
JavaScript
import { thenable } from '../../utils/promise/thenable';
import { getMatching, keyParser } from '../../utils/key';
import { parser } from '../parser';
import { STANDARD_SEGMENT, RULE_BASED_SEGMENT, LARGE_SEGMENT } from '../../utils/constants';
export function ruleBasedSegmentMatcherContext(segmentName, storage, log) {
return function ruleBasedSegmentMatcher(_a, splitEvaluator) {
var key = _a.key, attributes = _a.attributes;
var matchingKey = getMatching(key);
function matchConditions(rbsegment) {
var conditions = rbsegment.conditions || [];
if (!conditions.length)
return false;
var evaluator = parser(log, conditions, storage);
var evaluation = evaluator(keyParser(key), undefined, undefined, undefined, attributes, splitEvaluator);
return thenable(evaluation) ?
evaluation.then(function (evaluation) { return evaluation ? true : false; }) :
evaluation ? true : false;
}
function isInExcludedSegment(_a) {
var type = _a.type, name = _a.name;
return type === STANDARD_SEGMENT ?
storage.segments.isInSegment(name, matchingKey) :
type === RULE_BASED_SEGMENT ?
ruleBasedSegmentMatcherContext(name, storage, log)({ key: key, attributes: attributes }, splitEvaluator) :
type === LARGE_SEGMENT && storage.largeSegments ?
storage.largeSegments.isInSegment(name, matchingKey) :
false;
}
function isExcluded(rbSegment) {
var excluded = rbSegment.excluded || {};
if (excluded.keys && excluded.keys.indexOf(matchingKey) !== -1)
return true;
return (excluded.segments || []).reduce(function (result, excludedSegment) {
return thenable(result) ?
result.then(function (result) { return result || isInExcludedSegment(excludedSegment); }) :
result || isInExcludedSegment(excludedSegment);
}, false);
}
function isInRBSegment(rbSegment) {
if (!rbSegment)
return false;
var excluded = isExcluded(rbSegment);
return thenable(excluded) ?
excluded.then(function (excluded) { return excluded ? false : matchConditions(rbSegment); }) :
excluded ? false : matchConditions(rbSegment);
}
var rbSegment = storage.rbSegments.get(segmentName);
return thenable(rbSegment) ?
rbSegment.then(isInRBSegment) :
isInRBSegment(rbSegment);
};
}