a positive integer dictating the length of each chunk that gets yielded.
your input array that needs to be yielded in chunks.
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, [])], [])
a generator function that slices your input
array
to smaller chunks of your desiredchunk_size
.note that the final chunk that gets yielded may be smaller than your
chunk_size
if it does not dividearray.length
precisely.