All files / array/operator transpose.ts

100% Statements 16/16
100% Branches 4/4
100% Functions 1/1
100% Lines 13/13

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 221x 16x 16x 11x 2x   9x 19x     9x 19x 1x   18x 38x       8x    
export const transpose = <T>(array: T[][]): T[][] => {
  const newArray: T[][] = [];
  const length = array[0].length;
  if (!length) {
    throw new Error();
  }
  for (let i = 0; i < length; i++) {
    newArray.push([]);
  }
 
  for (const item of array) {
    if (item.length !== length) {
      throw new Error();
    }
    for (let j = 0; j < length; j++) {
      newArray[j].push(item[j]);
    }
  }
 
  return newArray;
};