All files / util chunk.ts

100% Statements 12/12
100% Branches 6/6
100% Functions 1/1
100% Lines 12/12

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 131x 1x 1x 5x 5x 4x 4x 22x 4x 4x 4x 1x  
// https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_chunk
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const chunk = <T>(input: T[], size: number): T[][] => {
  // Eject early if we're attempting to divide up items into arrays of length zero.
  if (size === 0) return [];
  return input.reduce<T[][]>(
    (arr, item, i) =>
      i % size === 0 ? [...arr, [item]] : [...arr.slice(0, -1), [...arr.slice(-1)[0], item]],
    []
  );
};
export default chunk;