@splitsoftware/splitio-commons
Version:
Split JavaScript SDK common components
57 lines (56 loc) • 2.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ruleBasedSegmentMatcherContext = void 0;
var thenable_1 = require("../../utils/promise/thenable");
var key_1 = require("../../utils/key");
var parser_1 = require("../parser");
var constants_1 = require("../../utils/constants");
function ruleBasedSegmentMatcherContext(segmentName, storage, log) {
return function ruleBasedSegmentMatcher(_a, splitEvaluator) {
var key = _a.key, attributes = _a.attributes;
var matchingKey = (0, key_1.getMatching)(key);
function matchConditions(rbsegment) {
var conditions = rbsegment.conditions || [];
if (!conditions.length)
return false;
var evaluator = (0, parser_1.parser)(log, conditions, storage);
var evaluation = evaluator((0, key_1.keyParser)(key), undefined, undefined, undefined, attributes, splitEvaluator);
return (0, thenable_1.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 === constants_1.STANDARD_SEGMENT ?
storage.segments.isInSegment(name, matchingKey) :
type === constants_1.RULE_BASED_SEGMENT ?
ruleBasedSegmentMatcherContext(name, storage, log)({ key: key, attributes: attributes }, splitEvaluator) :
type === constants_1.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 (0, thenable_1.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 (0, thenable_1.thenable)(excluded) ?
excluded.then(function (excluded) { return excluded ? false : matchConditions(rbSegment); }) :
excluded ? false : matchConditions(rbSegment);
}
var rbSegment = storage.rbSegments.get(segmentName);
return (0, thenable_1.thenable)(rbSegment) ?
rbSegment.then(isInRBSegment) :
isInRBSegment(rbSegment);
};
}
exports.ruleBasedSegmentMatcherContext = ruleBasedSegmentMatcherContext;