🧩 Splits an array into groups where consecutive elements satisfy the grouping predicate.
Syntax
TypeScript
import { chunkWhile } from '@opentf/std'; chunkWhile<T>(arr: T[] = [], predicate: (curr: T, next: T, index: number, arr: T[]) => boolean): T[][]
Parameters
arr: The source array.predicate: The function that determines if two consecutive elements belong to the same chunk. Receives(previous, current, index, array).
Returns
A new array containing the chunks.
Examples
TypeScript
chunkWhile([1, 2, 4, 5, 7], (a, b) => b - a < 2) //=> [[1, 2], [4, 5], [7]] chunkWhile([1, 2, 3, 4, 5], (a, b) => b - a === 1) //=> [[1, 2, 3, 4, 5]] chunkWhile([1, 1, 2, 3, 5, 5, 6], (a, b) => a === b) //=> [[1, 1], [2], [3], [5, 5], [6]] chunkWhile([1, 3, 5], () => false) //=> [[1], [3], [5]]