@jonahsnider/util

    Function chunk

    • Divides an array into several chunks of size.

      Time complexity: O(n)

      Space complexity: O(n)

      Type Parameters

      • T

      Parameters

      • array: readonly T[]

        The array to chunk

      • size: number

        The size of each chunk

      Returns Table<T>

      The new array containing chunks of the original array

      Array

      chunk([1, 2, 3, 4, 5, 6], 2); // [[1, 2], [3, 4], [5, 6]]
      
      chunk([1, 2, 3, 4, 5, 6], 5); // [[1, 2, 3, 4, 5], [6]]
      
      chunk([1, 2, 3, 4, 5, 6], 6); // [[1], [2], [3], [4], [5], [6]]
      
      chunk([1, 2, 3, 4, 5, 6], 100); // [[1, 2, 3, 4, 5, 6]]
      
    • Divides an iterable into several chunks of size.

      Time complexity: O(n)

      Space complexity: O(n)

      Type Parameters

      • T

      Parameters

      • iterable: Iterable<T>

        The iterable to chunk

      • size: number

        The size of each chunk

      Returns IterableIterator<T[]>

      An iterator yieliding new arrays containing chunks of the original iterable

      Iterable

      [...chunk(new Set([1, 2, 3, 4, 5, 6]), 2)]; // [[1, 2], [3, 4], [5, 6]]
      
      [...chunk(new Set([1, 2, 3, 4, 5, 6]), 5)]; // [[1, 2, 3, 4, 5], [6]]
      
      [...chunk(new Set([1, 2, 3, 4, 5, 6]), 6)]; // [[1], [2], [3], [4], [5], [6]]
      
      [...chunk(new Set([1, 2, 3, 4, 5, 6]), 100)]; // [[1, 2, 3, 4, 5, 6]]
      
    MMNEPVFCICPMFPCPTTAAATR