Skip to content

Commit

Permalink
feat: add new evaluateConditions method to EvaluationEngine (#136)
Browse files Browse the repository at this point in the history
* feat: add new evaluateConditions method to EvaluationEngine

* chore: add comments

* refactor: use for loops instead of some/every
  • Loading branch information
pjhul authored Oct 31, 2024
1 parent cf3ab2c commit 9566ac2
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions packages/experiment-core/src/evaluation/evaluation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,43 @@ export class EvaluationEngine {
return undefined;
}
}

const match = this.evaluateConditions(target, segment.conditions);

// On match, bucket the user.
if (match) {
const variantKey = this.bucket(target, segment);
if (variantKey !== undefined) {
return flag.variants[variantKey];
} else {
return undefined;
}
}

return undefined;
}

public evaluateConditions(
target: EvaluationTarget,
conditions: EvaluationCondition[][],
): boolean {
// Outer list logic is "or" (||)
for (const conditions of segment.conditions) {
for (const innerConditions of conditions) {
let match = true;
for (const condition of conditions) {

for (const condition of innerConditions) {
match = this.matchCondition(target, condition);
if (!match) {
break;
}
}
// On match, bucket the user.

if (match) {
const variantKey = this.bucket(target, segment);
if (variantKey !== undefined) {
return flag.variants[variantKey];
} else {
return undefined;
}
return true;
}
}
return undefined;

return false;
}

private matchCondition(
Expand Down

0 comments on commit 9566ac2

Please sign in to comment.