UNPKG

flagsmith-nodejs

Version:

Flagsmith lets you manage features flags and remote config across web, mobile and server side applications. Deliver true Continuous Integration. Get builds out faster. Control who has access to new features.

38 lines (37 loc) 2.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.traitsMatchSegmentCondition = exports.evaluateIdentityInSegment = exports.getIdentitySegments = void 0; const index_js_1 = require("../utils/hashing/index.js"); const constants_js_1 = require("./constants.js"); function getIdentitySegments(environment, identity, overrideTraits) { return environment.project.segments.filter(segment => evaluateIdentityInSegment(identity, segment, overrideTraits)); } exports.getIdentitySegments = getIdentitySegments; function evaluateIdentityInSegment(identity, segment, overrideTraits) { return (segment.rules.length > 0 && segment.rules.filter(rule => traitsMatchSegmentRule(overrideTraits || identity.identityTraits, rule, segment.id, identity.djangoID || identity.compositeKey)).length === segment.rules.length); } exports.evaluateIdentityInSegment = evaluateIdentityInSegment; function traitsMatchSegmentRule(identityTraits, rule, segmentId, identityId) { const matchesConditions = rule.conditions.length > 0 ? rule.matchingFunction()(rule.conditions.map(condition => traitsMatchSegmentCondition(identityTraits, condition, segmentId, identityId))) : true; return (matchesConditions && rule.rules.filter(rule => traitsMatchSegmentRule(identityTraits, rule, segmentId, identityId)).length === rule.rules.length); } function traitsMatchSegmentCondition(identityTraits, condition, segmentId, identityId) { if (condition.operator == constants_js_1.PERCENTAGE_SPLIT) { var hashedPercentage = (0, index_js_1.getHashedPercentateForObjIds)([segmentId, identityId]); return hashedPercentage <= parseFloat(String(condition.value)); } const traits = identityTraits.filter(t => t.traitKey === condition.property_); const trait = traits.length > 0 ? traits[0] : undefined; if (condition.operator === constants_js_1.IS_SET) { return !!trait; } else if (condition.operator === constants_js_1.IS_NOT_SET) { return trait == undefined; } return trait ? condition.matchesTraitValue(trait.traitValue) : false; } exports.traitsMatchSegmentCondition = traitsMatchSegmentCondition;