All files / utils extractKeys.ts

63.93% Statements 39/61
42.1% Branches 8/19
56.25% Functions 9/16
63.93% Lines 39/61

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                1x 2x     2x 2x 2x 2x           2x                     2x 2x 2x                                   2x     1x     12x   12x 2x 4x 4x         12x 4x 2x 4x       4x           4x             12x 8x     12x     1x     12x   12x 6x 6x 6x         12x       12x 6x     12x           12x           12x           12x    
import type {
  Condition,
  AttributeKey,
  GroupSegment,
  SegmentKey,
  Feature,
} from "@featurevisor/types";
 
export function extractSegmentsFromFeature(feature: Feature): Set<SegmentKey> {
  const result = new Set<SegmentKey>();
 
  // rules
  for (const traffic of feature.traffic) {
    if (traffic.segments) {
      extractSegmentKeysFromGroupSegments(traffic.segments).forEach((segmentKey) => {
        result.add(segmentKey);
      });
    }
  }
 
  // force
  Iif (feature.force) {
    for (const f of feature.force) {
      Iif (f.segments) {
        extractSegmentKeysFromGroupSegments(f.segments).forEach((segmentKey) => {
          result.add(segmentKey);
        });
      }
    }
  }
 
  // variable overrides from inside variations
  if (feature.variations) {
    for (const variation of feature.variations) {
      Iif (variation.variableOverrides) {
        for (const variableKey in variation.variableOverrides) {
          const overrides = variation.variableOverrides[variableKey];
 
          Iif (overrides) {
            for (const override of overrides) {
              Iif (override.segments) {
                extractSegmentKeysFromGroupSegments(override.segments).forEach((segmentKey) => {
                  result.add(segmentKey);
                });
              }
            }
          }
        }
      }
    }
  }
 
  return result;
}
 
export function extractSegmentKeysFromGroupSegments(
  segments: GroupSegment | GroupSegment[],
): Set<SegmentKey> {
  const result = new Set<SegmentKey>();
 
  if (Array.isArray(segments)) {
    segments.forEach((segment) => {
      extractSegmentKeysFromGroupSegments(segment).forEach((segmentKey) => {
        result.add(segmentKey);
      });
    });
  }
 
  if (typeof segments === "object") {
    if ("and" in segments) {
      extractSegmentKeysFromGroupSegments(segments.and).forEach((segmentKey) => {
        result.add(segmentKey);
      });
    }
 
    Iif ("or" in segments) {
      extractSegmentKeysFromGroupSegments(segments.or).forEach((segmentKey) => {
        result.add(segmentKey);
      });
    }
 
    Iif ("not" in segments) {
      extractSegmentKeysFromGroupSegments(segments.not).forEach((segmentKey) => {
        result.add(segmentKey);
      });
    }
  }
 
  if (typeof segments === "string") {
    result.add(segments);
  }
 
  return result;
}
 
export function extractAttributeKeysFromConditions(
  conditions: Condition | Condition[],
): Set<AttributeKey> {
  const result = new Set<AttributeKey>();
 
  if (Array.isArray(conditions)) {
    conditions.forEach((condition) => {
      extractAttributeKeysFromConditions(condition).forEach((attributeKey) => {
        result.add(attributeKey);
      });
    });
  }
 
  Iif (typeof conditions === "string") {
    return result;
  }
 
  if ("attribute" in conditions) {
    result.add(conditions.attribute);
  }
 
  Iif ("and" in conditions) {
    extractAttributeKeysFromConditions(conditions.and).forEach((attributeKey) => {
      result.add(attributeKey);
    });
  }
 
  Iif ("or" in conditions) {
    extractAttributeKeysFromConditions(conditions.or).forEach((attributeKey) => {
      result.add(attributeKey);
    });
  }
 
  Iif ("not" in conditions) {
    extractAttributeKeysFromConditions(conditions.not).forEach((attributeKey) => {
      result.add(attributeKey);
    });
  }
 
  return result;
}