All files / src/builder applyContextToConditions.ts

100% Statements 63/63
94.44% Branches 34/36
100% Functions 15/15
100% Lines 54/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140              4x     143x         142x 142x   142x     28x     496x 219x     277x   129x     129x     110x 73x     37x     167x 167x 99x 99x     39x 19x     20x         128x 69x 69x     28x 5x     23x         100x 30x 30x         13x 5x         8x           87x     33x         438x 7x     431x 124x     319x   319x 241x   241x 160x         159x 37x 70x       122x 29x 54x       93x 12x 16x         81x    
import type {
  Condition,
  AndCondition,
  OrCondition,
  NotCondition,
  Context,
} from "@featurevisor/types";
import { allConditionsAreMatched } from "@featurevisor/sdk";
import type { Featurevisor } from "@featurevisor/sdk";
 
export function applyContextToConditions(
  featurevisor: Featurevisor,
  conditions: Condition | Condition[],
  context: Context,
): Condition | Condition[] {
  const withContextApplied = applyContextToCondition(featurevisor, conditions, context);
  const removed = removeRedundantConditions(withContextApplied);
 
  return removed;
}
 
export function removeRedundantConditions(
  conditions: Condition | Condition[],
): Condition | Condition[] {
  if (conditions === "*") {
    return conditions;
  }
 
  if (Array.isArray(conditions)) {
    // Recursively process each condition
    const processed = conditions.map((c) => removeRedundantConditions(c)) as Condition[];
 
    // Filter out "*" values
    const filtered = processed.filter((c) => c !== "*");
 
    // If all were "*", return "*"
    if (filtered.length === 0) {
      return "*";
    }
 
    return filtered;
  }
 
  Eif (typeof conditions === "object") {
    if ("and" in conditions) {
      const processed = conditions.and.map((c) => removeRedundantConditions(c)) as Condition[];
      const filtered = processed.filter((c) => c !== "*");
 
      // If all were "*", return "*"
      if (filtered.length === 0) {
        return "*";
      }
 
      return {
        and: filtered,
      } as AndCondition;
    }
 
    if ("or" in conditions) {
      const processed = conditions.or.map((c) => removeRedundantConditions(c)) as Condition[];
      const filtered = processed.filter((c) => c !== "*");
 
      // If all were "*", return "*"
      if (filtered.length === 0) {
        return "*";
      }
 
      return {
        or: filtered,
      } as OrCondition;
    }
 
    if ("not" in conditions) {
      const processed = conditions.not.map((c) => removeRedundantConditions(c)) as Condition[];
      const filtered = processed.filter((c) => c !== "*");
 
      // `not` negates the implicit AND of its children. Matched children are true
      // and can be removed: not(true && X) becomes not(X). If every child was
      // true, keep not("*") so it remains an always-false expression.
      if (filtered.length === 0) {
        return {
          not: ["*"],
        } as NotCondition;
      }
 
      return {
        not: filtered,
      } as NotCondition;
    }
  }
 
  return conditions;
}
 
export function applyContextToCondition(
  featurevisor: Featurevisor,
  condition: Condition | Condition[],
  context: Context,
): Condition | Condition[] {
  if (condition === "*") {
    return condition;
  }
 
  if (Array.isArray(condition)) {
    return condition.map((c) => applyContextToCondition(featurevisor, c, context)) as Condition[];
  }
 
  Eif (typeof condition === "object") {
    // plain condition
    if ("attribute" in condition) {
      const matched = allConditionsAreMatched(condition, context);
 
      if (matched) {
        return "*";
      }
    }
 
    // AND, OR, NOT conditions
    if ("and" in condition) {
      return {
        and: condition.and.map((c) => applyContextToCondition(featurevisor, c, context)),
      } as AndCondition;
    }
 
    if ("or" in condition) {
      return {
        or: condition.or.map((c) => applyContextToCondition(featurevisor, c, context)),
      } as OrCondition;
    }
 
    if ("not" in condition) {
      return {
        not: condition.not.map((c) => applyContextToCondition(featurevisor, c, context)),
      } as NotCondition;
    }
  }
 
  return condition;
}