All files / src utils.ts

98.31% Statements 58/59
95.45% Branches 21/22
100% Functions 15/15
98.18% Lines 54/55

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                      1x 7x 7x 20x 5x   15x 6x 6x   9x                                   1x 13x 11x   2x         160x 65x     95x 95x 95x     1x 70x                         1x 200x       200x 89x     111x 155x 86x       25x                         1x 132x 67x       65x   160x 65x       95x     95x 88x 88x       95x       1x 11x 11x 22x   11x     47x 12x   23x   34x 78x                                       1x 176x 176x 232x   176x    
/**
 * Returns a new array with the first occurrence of an element removed from
 * the array passed.
 *
 * @param {T[]} elems
 *   The original array.
 * @param {T} elem
 *   The element to remove
 * @return {T[]}
 *   A new array with the element removed if original array contained it.
 */
export function removeFirst<T>(elems: T[], elem: T): T[] {
  let found = false;
  return elems.filter(e => {
    if (found) {
      return true;
    } else {
      if (e === elem) {
        found = true;
        return false;
      } else {
        return true;
      }
    }
  });
}
 
/**
 * Returns a new array with element at index `idx` replaced with `elem`.
 *
 * @param {T[]} elems
 *   The original array.
 * @param {number} idx
 *   The index at which to replace.
 * @param {T} elem
 *   The element to place at the specified index.
 * @return {T[]}
 *   A new array with the element replaced.
 */
export function replace<T>(elems: T[], idx: number, elem: T): T[] {
  if (idx >= 0 && idx < elems.length) {
    return [...elems.slice(0, idx), elem, ...elems.slice(idx + 1)];
  } else {
    return [...elems];
  }
}
 
function tails<T>(elems: T[]): T[][] {
  if (elems.length === 0) {
    return [[]];
  }
 
  const a = tails(elems.slice(1));
  a.unshift(elems.slice(0));
  return a;
}
 
export function flatten<T>(elems: T[][]): T[] {
  return [].concat.apply([], elems);
}
 
/**
 * Compare 2 arrays using `===`. No deep comparaison.
 *
 * @param {T[]} a
 *   First array
 * @param {T[]} b
 *   Second array
 * @return {boolean}
 *   True if `a` and `b` contains the same values in the same order.
 */
export function arrayEquals<T>(a: T[], b: T[]): boolean {
  Iif (!(Array.isArray(a) && Array.isArray(b))) {
    return false;
  }
 
  if (a.length !== b.length) {
    return false;
  }
 
  for (let i = 0; i < a.length; i++) {
    if (a[i] !== b[i]) {
      return false;
    }
  }
 
  return true;
}
 
/**
 * Returns combinations of elements from an array.
 *
 * @param {number} k
 *   The number of elements in each combination.
 * @param {T[]} elems
 *   The array of elements to pull from.
 * @return {T[][]}
 *   All possible combinations of `k` elements among `elems`.
 */
export function combinations<T>(k: number, elems: T[]): T[][] {
  if (k <= 0) {
    return [[]];
  }
 
  // Get all tails
  return tails(elems).reduce((acc, tailOfElems) => {
    // For each tail ...
    if (tailOfElems.length === 0) {
      return acc;
    }
 
    // Recursion : get all combinations of (k-1) elements with end elements
    const tailCombinations = combinations(k - 1, tailOfElems.slice(1));
 
    // Prepend first element to all combinations
    const comb = tailCombinations.map(tailCombination => {
      tailCombination.unshift(tailOfElems[0]);
      return tailCombination;
    });
 
    // Add to result
    return acc.concat(comb);
  }, [] as T[][]);
}
 
export function allCombinations<T>(elems: T[]): T[][] {
  let allComb: T[][] = [];
  for (let i = 0; i <= elems.length; i++) {
    allComb = allComb.concat(combinations(i, elems));
  }
  return allComb;
}
 
export function cartesianProduct<T>(...sets: T[][]): T[][] {
  return sets.reduce(
    (acc: T[][], set: T[]) => {
      return flatten(
        acc.map((x: T[]) => {
          return set.map((y: T) => {
            return [...x, y];
          });
        })
      );
    },
    [[]] as T[][]
  );
}
 
/**
 * Compute the fractional n!
 *
 * Here we are using an iterative version of this function for better
 * performance over a recursive version.
 *
 * @param {number} n
 *   The number to compute the factorial from
 * @return {number}
 *   The result of n!
 */
export function factorial(n: number): number {
  let returnValue = 1;
  for (let i = 2; i <= n; i++) {
    returnValue = returnValue * i;
  }
  return returnValue;
}