All files feature.ts

95.55% Statements 43/45
90.47% Branches 19/21
100% Functions 7/7
95.45% Lines 42/44

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    1x 1x     1x       39x       39x 65x   65x 37x       2x     1x 147x 5x     142x     1x           56x 81x               29x     52x     56x 51x     5x               1x             39x 59x               39x             39x   39x                     1x           108x         108x 51x     57x 153x   153x       13x 13x 13x     140x       1x 1x 1x       57x    
import { Allocation, Context, Traffic, Feature, Force } from "@featurevisor/types";
import { DatafileReader } from "./datafileReader";
import { allGroupSegmentsAreMatched } from "./segments";
import { allConditionsAreMatched } from "./conditions";
import { Logger } from "./logger";
 
export function getMatchedAllocation(
  traffic: Traffic,
  bucketValue: number,
): Allocation | undefined {
  Iif (!traffic.allocation) {
    return undefined;
  }
 
  for (const allocation of traffic.allocation) {
    const [start, end] = allocation.range;
 
    if (allocation.range && start <= bucketValue && end >= bucketValue) {
      return allocation;
    }
  }
 
  return undefined;
}
 
export function parseFromStringifiedSegments(value) {
  if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) {
    return JSON.parse(value);
  }
 
  return value;
}
 
export function getMatchedTraffic(
  traffic: Traffic[],
  context: Context,
  datafileReader: DatafileReader,
  logger: Logger,
): Traffic | undefined {
  const matchedTraffic = traffic.find((t) => {
    if (
      !allGroupSegmentsAreMatched(
        parseFromStringifiedSegments(t.segments),
        context,
        datafileReader,
        logger,
      )
    ) {
      return false;
    }
 
    return true;
  });
 
  if (matchedTraffic && matchedTraffic.percentage > 0) {
    return matchedTraffic;
  }
 
  return;
}
 
export interface MatchedTrafficAndAllocation {
  matchedTraffic: Traffic | undefined;
  matchedAllocation: Allocation | undefined;
}
 
export function getMatchedTrafficAndAllocation(
  traffic: Traffic[],
  context: Context,
  bucketValue: number,
  datafileReader: DatafileReader,
  logger: Logger,
): MatchedTrafficAndAllocation {
  const matchedTraffic = traffic.find((t) => {
    return allGroupSegmentsAreMatched(
      parseFromStringifiedSegments(t.segments),
      context,
      datafileReader,
      logger,
    );
  });
 
  Iif (!matchedTraffic) {
    return {
      matchedTraffic: undefined,
      matchedAllocation: undefined,
    };
  }
 
  const matchedAllocation = getMatchedAllocation(matchedTraffic, bucketValue);
 
  return {
    matchedTraffic,
    matchedAllocation,
  };
}
 
export interface ForceResult {
  force?: Force;
  forceIndex?: number;
}
 
export function findForceFromFeature(
  feature: Feature,
  context: Context,
  datafileReader: DatafileReader,
  logger: Logger,
): ForceResult {
  const result: ForceResult = {
    force: undefined,
    forceIndex: undefined,
  };
 
  if (!feature.force) {
    return result;
  }
 
  for (let i = 0; i < feature.force.length; i++) {
    const currentForce = feature.force[i];
 
    if (
      currentForce.conditions &&
      allConditionsAreMatched(currentForce.conditions, context, logger)
    ) {
      result.force = currentForce;
      result.forceIndex = i;
      break;
    }
 
    if (
      currentForce.segments &&
      allGroupSegmentsAreMatched(currentForce.segments, context, datafileReader, logger)
    ) {
      result.force = currentForce;
      result.forceIndex = i;
      break;
    }
  }
 
  return result;
}