Skip to content

Commit

Permalink
Lazily calculate the hash (#130)
Browse files Browse the repository at this point in the history
* Lazily calculate the hash

* Refactor to switch statement
  • Loading branch information
eldar-gamisoniya authored Aug 7, 2023
1 parent 9e4bbab commit 53f9f57
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions flagsmith-engine/features/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,27 @@ export class FeatureStateModel {
}
return this.featureSegment.priority < other.featureSegment.priority;
}

getMultivariateValue(identityID: number | string) {
const percentageValue = getHashedPercentateForObjIds([
this.djangoID || this.featurestateUUID,
identityID
]);

getMultivariateValue(identityID: number | string) {
let percentageValue: number | undefined;
let startPercentage = 0;
const sortedF = this.multivariateFeatureStateValues.sort((a, b) =>{
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 = getHashedPercentateForObjIds([
this.djangoID || this.featurestateUUID,
identityID
]);
}
}
const limit = myValue.percentageAllocation + startPercentage;
if (startPercentage <= percentageValue && percentageValue < limit) {
return myValue.multivariateFeatureOption.value;
Expand Down

0 comments on commit 53f9f57

Please sign in to comment.