All files / builder buildScopedDatafile.ts

98.85% Statements 86/87
100% Branches 25/25
100% Functions 2/2
98.79% Lines 82/83

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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230  1x   1x 1x     53x 26x     27x 27x 25x     2x 2x           1x       76x         76x       76x     76x 86x 86x 86x         86x   86x 60x         76x 64x     64x 19x 19x     19x 13x                 19x 7x                   64x 64x 64x 42x     42x     42x 42x 42x                 42x 9x 9x   9x 9x       11x     11x 7x 7x                 11x 4x 4x             11x               64x 21x   21x 21x     21x 20x 20x   20x 20x       21x     21x 16x                 21x 5x             21x         21x         64x 64x       64x 42x 42x   42x         7x     42x 35x   35x       64x     64x       76x 60x     76x    
import { DatafileContent, Context, Traffic } from "@featurevisor/types";
import { DatafileReader, createLogger } from "@featurevisor/sdk";
 
import { buildScopedConditions } from "./buildScopedConditions";
import { buildScopedSegments } from "./buildScopedSegments";
 
function parseIfStringified<T>(value: T): T {
  if (typeof value !== "string" || value === "*") {
    return value;
  }
 
  const firstChar = value[0];
  if (firstChar !== "{" && firstChar !== "[") {
    return value;
  }
 
  try {
    return JSON.parse(value) as T;
  } catch {
    return value;
  }
}
 
export function buildScopedDatafile(
  originalDatafileContent: DatafileContent,
  context: Context,
): DatafileContent {
  const originalDatafileReader = new DatafileReader({
    datafile: originalDatafileContent,
    logger: createLogger({ level: "fatal" }),
  });
 
  const scopedDatafileContent: DatafileContent = JSON.parse(
    JSON.stringify(originalDatafileContent),
  );
 
  const removeSegments: string[] = [];
 
  // segments
  for (const segmentKey in scopedDatafileContent.segments) {
    const segment = scopedDatafileContent.segments[segmentKey];
    const originalConditions = segment.conditions;
    const scopedConditions = buildScopedConditions(
      originalDatafileReader,
      originalConditions,
      context,
    );
    scopedDatafileContent.segments[segmentKey].conditions = scopedConditions;
 
    if (scopedConditions === "*") {
      removeSegments.push(segmentKey);
    }
  }
 
  // features
  for (const featureKey in scopedDatafileContent.features) {
    const feature = scopedDatafileContent.features[featureKey];
 
    // force
    if (feature.force) {
      for (let forceI = 0; forceI < feature.force.length; forceI++) {
        const force = feature.force[forceI];
 
        // segments
        if (force.segments) {
          feature.force[forceI].segments = buildScopedSegments(
            originalDatafileReader,
            force.segments,
            context,
            removeSegments,
          );
        }
 
        // conditions
        if (force.conditions) {
          feature.force[forceI].conditions = buildScopedConditions(
            originalDatafileReader,
            force.conditions,
            context,
          );
        }
      }
    }
 
    // traffic
    const originalTrafficSegments: (string | string[] | object | undefined)[] = [];
    if (feature.traffic) {
      for (let trafficI = 0; trafficI < feature.traffic.length; trafficI++) {
        const traffic = feature.traffic[trafficI];
 
        // Store original segments before scoping
        originalTrafficSegments[trafficI] = traffic.segments;
 
        // segments
        if (traffic.segments) {
          const segments = parseIfStringified(traffic.segments);
          feature.traffic[trafficI].segments = buildScopedSegments(
            originalDatafileReader,
            segments,
            context,
            removeSegments,
          );
        }
 
        // variable overrides
        if (traffic.variableOverrides) {
          for (const variableKey in traffic.variableOverrides) {
            const variableOverrides = traffic.variableOverrides[variableKey];
 
            for (
              let variableOverrideI = 0;
              variableOverrideI < variableOverrides.length;
              variableOverrideI++
            ) {
              const variableOverride = variableOverrides[variableOverrideI];
 
              // segments
              if (variableOverride.segments) {
                const segments = parseIfStringified(variableOverride.segments);
                variableOverride.segments = buildScopedSegments(
                  originalDatafileReader,
                  segments,
                  context,
                  removeSegments,
                );
              }
 
              // conditions
              if (variableOverride.conditions) {
                const conditions = parseIfStringified(variableOverride.conditions);
                variableOverride.conditions = buildScopedConditions(
                  originalDatafileReader,
                  conditions,
                  context,
                );
              }
 
              variableOverrides[variableOverrideI] = variableOverride;
            }
          }
        }
      }
    }
 
    // variation
    if (feature.variations) {
      const variations = feature.variations;
 
      for (let variationI = 0; variationI < variations.length; variationI++) {
        const variation = variations[variationI];
 
        // variable overrides
        if (variation.variableOverrides) {
          for (const variableKey in variation.variableOverrides) {
            const variableOverrides = variation.variableOverrides[variableKey];
 
            for (
              let variableOverrideI = 0;
              variableOverrideI < variableOverrides.length;
              variableOverrideI++
            ) {
              const variableOverride = variableOverrides[variableOverrideI];
 
              // segments
              if (variableOverride.segments) {
                variableOverride.segments = buildScopedSegments(
                  originalDatafileReader,
                  variableOverride.segments,
                  context,
                  removeSegments,
                );
              }
 
              // conditions
              if (variableOverride.conditions) {
                variableOverride.conditions = buildScopedConditions(
                  originalDatafileReader,
                  variableOverride.conditions,
                  context,
                );
              }
 
              variableOverrides[variableOverrideI] = variableOverride;
            }
          }
        }
 
        feature.variations[variationI] = variation;
      }
    }
 
    // feature traffic consecutive segments with `*` removal
    if (feature.traffic) {
      const newTrafficArray: Traffic[] = [];
 
      let lastAddedTraffic: Traffic | undefined;
 
      for (let i = 0; i < feature.traffic.length; i++) {
        let shouldAdd = true;
        const currentTraffic = feature.traffic[i];
 
        if (
          lastAddedTraffic &&
          lastAddedTraffic.segments === "*" &&
          currentTraffic.segments === "*"
        ) {
          shouldAdd = false;
        }
 
        if (shouldAdd) {
          newTrafficArray.push(currentTraffic);
 
          lastAddedTraffic = currentTraffic;
        }
      }
 
      feature.traffic = newTrafficArray;
    }
 
    scopedDatafileContent.features[featureKey] = feature;
  }
 
  // remove segments
  for (const removeSegment of removeSegments) {
    delete scopedDatafileContent.segments[removeSegment];
  }
 
  return scopedDatafileContent;
}