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.

119 lines (118 loc) 4.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FeatureSegment = exports.FeatureStateModel = exports.MultivariateFeatureStateValueModel = exports.MultivariateFeatureOptionModel = exports.FeatureModel = void 0; const node_crypto_1 = require("node:crypto"); const index_js_1 = require("../utils/hashing/index.js"); class FeatureModel { id; name; type; constructor(id, name, type) { this.id = id; this.name = name; this.type = type; } eq(other) { return !!other && this.id === other.id; } } exports.FeatureModel = FeatureModel; class MultivariateFeatureOptionModel { value; id; constructor(value, id) { this.value = value; this.id = id; } } exports.MultivariateFeatureOptionModel = MultivariateFeatureOptionModel; class MultivariateFeatureStateValueModel { multivariateFeatureOption; percentageAllocation; id; mvFsValueUuid = (0, node_crypto_1.randomUUID)(); constructor(multivariate_feature_option, percentage_allocation, id, mvFsValueUuid) { this.id = id; this.percentageAllocation = percentage_allocation; this.multivariateFeatureOption = multivariate_feature_option; this.mvFsValueUuid = mvFsValueUuid || this.mvFsValueUuid; } } exports.MultivariateFeatureStateValueModel = MultivariateFeatureStateValueModel; class FeatureStateModel { feature; enabled; djangoID; featurestateUUID = (0, node_crypto_1.randomUUID)(); featureSegment; value; multivariateFeatureStateValues = []; constructor(feature, enabled, djangoID, value, featurestateUuid = (0, node_crypto_1.randomUUID)()) { this.feature = feature; this.enabled = enabled; this.djangoID = djangoID; this.value = value; this.featurestateUUID = featurestateUuid; } setValue(value) { this.value = value; } getValue(identityId) { if (!!identityId && this.multivariateFeatureStateValues.length > 0) { return this.getMultivariateValue(identityId); } return this.value; } /* Returns `True` if `this` is higher segment priority than `other` (i.e. has lower value for featureSegment.priority) NOTE: A segment will be considered higher priority only if: 1. `other` does not have a feature segment(i.e: it is an environment feature state or it's a feature state with feature segment but from an old document that does not have `featureSegment.priority`) but `this` does. 2. `other` have a feature segment with high priority */ isHigherSegmentPriority(other) { if (!other.featureSegment || !this.featureSegment) { return !!this.featureSegment && !other.featureSegment; } return this.featureSegment.priority < other.featureSegment.priority; } getMultivariateValue(identityID) { let percentageValue; let startPercentage = 0; const sortedF = this.multivariateFeatureStateValues.sort((a, b) => { return a.id - b.id; }); for (const myValue of sortedF) { switch (myValue.percentageAllocation) { case 0: continue; case 100: return myValue.multivariateFeatureOption.value; default: if (percentageValue === undefined) { percentageValue = (0, index_js_1.getHashedPercentageForObjIds)([ this.djangoID || this.featurestateUUID, identityID ]); } } const limit = myValue.percentageAllocation + startPercentage; if (startPercentage <= percentageValue && percentageValue < limit) { return myValue.multivariateFeatureOption.value; } startPercentage = limit; } return this.value; } } exports.FeatureStateModel = FeatureStateModel; class FeatureSegment { priority; constructor(priority) { this.priority = priority; } } exports.FeatureSegment = FeatureSegment;