StreamClass.prototype.chunkWhile<A, C>(initial: A,step: (value: T,accumulator: A) => { acc: A; emit?: C; halt?: boolean; },flush?: (accumulator: A) => C | undefined): StreamClass<C, E>
The general chunker — Elixir's chunk_while/4, object-shaped for JS: step returns
{ acc } to keep accumulating, { acc, emit } to emit a chunk, plus halt: true to
end the stream; flush may emit one last chunk from the final accumulator. Values
only; failures pass through.
const batchesOfTwo = Stream.from([1, 2, 3, 4, 5]).chunkWhile( [] as number[], (n, acc) => (acc.length === 1 ? { acc: [], emit: [...acc, n] } : { acc: [...acc, n] }), (acc) => (acc.length > 0 ? acc : undefined), ); await batchesOfTwo.collect(); // [[1, 2], [3, 4], [5]]
StreamClass<C, E>