All files / builder buildScopedConditions.ts

100% Statements 62/62
100% Branches 18/18
100% Functions 15/15
100% Lines 53/53

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                  2x         134x 134x   134x     2x     480x 213x     267x   121x     121x     102x 67x     35x     165x 165x 99x 99x     39x 19x     20x         126x 69x 69x     28x 5x     23x         98x 30x 30x     13x 7x     6x           85x     2x         422x 7x     415x 116x     311x   311x 233x   233x 154x         157x 37x 70x       120x 29x 54x       91x 12x 16x         79x    
import type {
  Condition,
  AndCondition,
  OrCondition,
  NotCondition,
  Context,
} from "@featurevisor/types";
import type { DatafileReader } from "@featurevisor/sdk";
 
export function buildScopedConditions(
  datafileReader: DatafileReader,
  conditions: Condition | Condition[],
  context: Context,
): Condition | Condition[] {
  const scoped = buildScopedCondition(datafileReader, conditions, context);
  const removed = removeRedundantConditions(scoped);
 
  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;
  }
 
  if (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 !== "*");
 
      // If all were "*", return "*"
      if (filtered.length === 0) {
        return "*";
      }
 
      return {
        not: filtered,
      } as NotCondition;
    }
  }
 
  return conditions;
}
 
export function buildScopedCondition(
  datafileReader: DatafileReader,
  condition: Condition | Condition[],
  context: Context,
): Condition | Condition[] {
  if (condition === "*") {
    return condition;
  }
 
  if (Array.isArray(condition)) {
    return condition.map((c) => buildScopedCondition(datafileReader, c, context)) as Condition[];
  }
 
  if (typeof condition === "object") {
    // plain condition
    if ("attribute" in condition) {
      const matched = datafileReader.allConditionsAreMatched(condition, context);
 
      if (matched) {
        return "*";
      }
    }
 
    // AND, OR, NOT conditions
    if ("and" in condition) {
      return {
        and: condition.and.map((c) => buildScopedCondition(datafileReader, c, context)),
      } as AndCondition;
    }
 
    if ("or" in condition) {
      return {
        or: condition.or.map((c) => buildScopedCondition(datafileReader, c, context)),
      } as OrCondition;
    }
 
    if ("not" in condition) {
      return {
        not: condition.not.map((c) => buildScopedCondition(datafileReader, c, context)),
      } as NotCondition;
    }
  }
 
  return condition;
}