@oazmi/kitchensink - v0.9.13
    Preparing search index...

    Function chunkGenerator

    • a generator function that slices your input array to smaller chunks of your desired chunk_size.

      note that the final chunk that gets yielded may be smaller than your chunk_size if it does not divide array.length precisely.

      Type Parameters

      • T

      Parameters

      • chunk_size: number

        a positive integer dictating the length of each chunk that gets yielded.

      • array: T[]

        your input array that needs to be yielded in chunks.

      Returns Generator<T[], void>

      a chunk of length chunk_size from your input array.

      import { assertEquals } from "jsr:@std/assert"

      const my_arr = rangeArray(0, 30) // equals to `[0, 1, 2, ..., 28, 29]`

      // below, we split `my_arr` into smaller array chunks of size `8`, except for the last chunk, which is smaller.
      assertEquals([...chunkGenerator(8, my_arr)], [
      [ 0, 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],
      ])

      // chunking zero length array will not yield anything
      assertEquals([...chunkGenerator(8, [])], [])