All files / src/bundle/utils array.ts

91.3% Statements 21/23
100% Branches 2/2
84.61% Functions 11/13
94.44% Lines 17/18

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 303x 74x     3x       3x 21x 42x 111x 78x   33x   111x     78x 14x   21x 111x 42x   21x 21x    
export function flatMap<T, R>(arr: Array<T>, mapper: (a: T) => Array<R>): Array<R> {
  return arr.map(mapper).reduce((acc, val) => acc.concat(val), []);
}
 
export function diffArray<T>(arr1: Array<T>, arr2: Array<T>) {
  return arr1.filter((val) => !arr2.includes(val));
}
 
export const extractDuplicates = (arr: string[][]): string[][] => {
  const duplicates: string[] = Object.entries(
    flatMap(arr, (items) => items).reduce((counter: { [classId: string]: number }, value) => {
      if (!counter[value]) {
        counter[value] = 1;
      } else {
        counter[value]++;
      }
      return counter;
    }, {}),
  )
    .filter(([_, count]) => count > 1)
    .map(([value]) => value);
 
  const filtered = arr
    .map((items) => items.filter((item) => !duplicates.includes(item)))
    .filter((items) => items.length > 0);
 
  duplicates.forEach((duplicate) => filtered.push([duplicate]));
  return filtered;
};