All files / src/utils extractKeys.ts

67.21% Statements 41/61
63.15% Branches 24/38
62.5% Functions 10/16
67.21% Lines 41/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                10x 10x     10x 12x 12x 14x           10x                     10x 7x 7x                                   10x     19x     72x   72x 15x 26x 26x         72x 30x 7x 14x       30x           30x 8x 12x         72x 42x     72x     26x     52x   52x 26x 26x 26x         52x       52x 26x     52x           52x           52x           52x    
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) {
    Eif (traffic.segments) {
      extractSegmentKeysFromGroupSegments(traffic.segments).forEach((segmentKey) => {
        result.add(segmentKey);
      });
    }
  }
 
  // force
  Iif (feature.force) {
    for (const f of feature.force) {
      if (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];
 
          if (overrides) {
            for (const override of overrides) {
              if (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);
      });
    }
 
    if ("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;
}