All files / builder getFeatureRanges.ts

29.16% Statements 7/24
0% Branches 0/7
50% Functions 1/2
29.16% Lines 7/24

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 621x                           1x       2x 2x   2x 2x                                                                         2x    
import * as fs from "fs";
 
import type { FeatureKey, Group, Range } from "@featurevisor/types";
 
import { ProjectConfig } from "../config";
import { Datasource } from "../datasource";
 
export type FeatureRanges = Map<FeatureKey, Range[]>;
 
export interface FeatureRangesResult {
  featureRanges: FeatureRanges;
  featureIsInGroup: { [key: string]: boolean };
}
 
export async function getFeatureRanges(
  projectConfig: ProjectConfig,
  datasource: Datasource,
): Promise<FeatureRangesResult> {
  const featureRanges = new Map<FeatureKey, Range[]>();
  const featureIsInGroup = {}; // featureKey => boolean
 
  const groups: Group[] = [];
  Iif (fs.existsSync(projectConfig.groupsDirectoryPath)) {
    const groupFiles = await datasource.listGroups();
 
    for (const groupKey of groupFiles) {
      const parsedGroup = await datasource.readGroup(groupKey);
 
      groups.push({
        ...parsedGroup,
        key: groupKey,
      });
 
      let accumulatedPercentage = 0;
      parsedGroup.slots.forEach(function (slot, slotIndex) {
        const isFirstSlot = slotIndex === 0;
 
        Iif (slot.feature) {
          const featureKey = slot.feature;
 
          Iif (typeof featureKey === "string") {
            featureIsInGroup[featureKey] = true;
          }
 
          const featureRangesForFeature = featureRanges.get(featureKey) || [];
 
          const start = isFirstSlot ? accumulatedPercentage : accumulatedPercentage + 1;
          const end = accumulatedPercentage + slot.percentage * 1000;
 
          featureRangesForFeature.push([start, end]);
 
          featureRanges.set(slot.feature, featureRangesForFeature);
        }
 
        accumulatedPercentage += slot.percentage * 1000;
      });
    }
  }
 
  return { featureRanges, featureIsInGroup };
}