All files events.ts

100% Statements 30/30
69.23% Branches 9/13
100% Functions 4/4
100% Lines 30/30

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          3x         4x 4x   4x 4x 8x     4x           3x       4x 4x   4x 4x     4x 4x 4x     4x 4x   1x   1x       3x 3x   3x   2x         4x 7x   4x         4x       7x   4x               4x    
import type { StickyFeatures, FeatureKey } from "@featurevisor/types";
 
import type { EventDetails } from "./emitter";
import type { DatafileReader } from "./datafileReader";
 
export function getParamsForStickySetEvent(
  previousStickyFeatures: StickyFeatures = {},
  newStickyFeatures: StickyFeatures = {},
  replace,
): EventDetails {
  const keysBefore = Object.keys(previousStickyFeatures);
  const keysAfter = Object.keys(newStickyFeatures);
 
  const allKeys = [...keysBefore, ...keysAfter];
  const uniqueFeaturesAffected = allKeys.filter(
    (element, index) => allKeys.indexOf(element) === index,
  );
 
  return {
    features: uniqueFeaturesAffected,
    replaced: replace,
  };
}
 
export function getParamsForDatafileSetEvent(
  previousDatafileReader: DatafileReader,
  newDatafileReader: DatafileReader,
): EventDetails {
  const previousRevision = previousDatafileReader.getRevision();
  const previousFeatureKeys = previousDatafileReader.getFeatureKeys();
 
  const newRevision = newDatafileReader.getRevision();
  const newFeatureKeys = newDatafileReader.getFeatureKeys();
 
  // results
  const removedFeatures: FeatureKey[] = [];
  const changedFeatures: FeatureKey[] = [];
  const addedFeatures: FeatureKey[] = [];
 
  // checking against existing datafile
  for (const previousFeatureKey of previousFeatureKeys) {
    if (newFeatureKeys.indexOf(previousFeatureKey) === -1) {
      // feature was removed in new datafile
      removedFeatures.push(previousFeatureKey);
 
      continue;
    }
 
    // feature exists in both datafiles, check if it was changed
    const previousFeature = previousDatafileReader.getFeature(previousFeatureKey);
    const newFeature = newDatafileReader.getFeature(previousFeatureKey);
 
    if (previousFeature?.hash !== newFeature?.hash) {
      // feature was changed in new datafile
      changedFeatures.push(previousFeatureKey);
    }
  }
 
  // checking against new datafile
  for (const newFeatureKey of newFeatureKeys) {
    if (previousFeatureKeys.indexOf(newFeatureKey) === -1) {
      // feature was added in new datafile
      addedFeatures.push(newFeatureKey);
    }
  }
 
  // combine all affected feature keys
  const allAffectedFeatures: FeatureKey[] = [
    ...removedFeatures,
    ...changedFeatures,
    ...addedFeatures,
  ].filter((element, index, array) => array.indexOf(element) === index);
 
  const details = {
    revision: newRevision,
    previousRevision,
    revisionChanged: previousRevision !== newRevision,
 
    features: allAffectedFeatures,
  };
 
  return details;
}