From 9566ac208a31b33bc1e6c34ad9bc8be1376bb745 Mon Sep 17 00:00:00 2001 From: Paul Hultgren <39424187+pjhul@users.noreply.github.com> Date: Thu, 31 Oct 2024 12:13:46 -0500 Subject: [PATCH] feat: add new `evaluateConditions` method to EvaluationEngine (#136) * feat: add new evaluateConditions method to EvaluationEngine * chore: add comments * refactor: use for loops instead of some/every --- .../src/evaluation/evaluation.ts | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/packages/experiment-core/src/evaluation/evaluation.ts b/packages/experiment-core/src/evaluation/evaluation.ts index 96608ab..0f7f1ae 100644 --- a/packages/experiment-core/src/evaluation/evaluation.ts +++ b/packages/experiment-core/src/evaluation/evaluation.ts @@ -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(