Iterator chunking
Overlapping/non-overlapping streams of configurable size.
| Type | TC39 | CoreJS | Babel | Civet | JS+ |
|---|---|---|---|---|---|
| Polyfill | ✅ Stage 2.7 View | ✅ View | ❌ | ❌ | ✅ via Custom |
Iterator.prototype.chunks<T>(chunkSize: number): [T[]]
Section titled “Iterator.prototype.chunks<T>(chunkSize: number): [T[]]”Splits the iterator into non-overlapping arrays of length chunkSize. If the iterator’s length is not evenly divisible, the final chunk contains the remaining elements.
[...[1, 2, 3, 4, 5].values().chunks(2)]// → [[1, 2], [3, 4], [5]]
[...[1, 2, 3].values().chunks(3)]// → [[1, 2, 3]]
[...[1, 2, 3].values().chunks(5)]// → [[1, 2, 3]]Iterator.prototype.windows<T>(windowSize: number, undersized?: 'allow-partial'): [T[]]
Section titled “Iterator.prototype.windows<T>(windowSize: number, undersized?: 'allow-partial'): [T[]]”Produces a sliding window over the iterator, yielding overlapping arrays of length windowSize. Each step advances by one element.
[...[1, 2, 3, 4, 5].values().windows(3)]// → [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
[...[1, 2, 3, 4].values().windows(3, "allow-partial")]// → [[1, 2, 3], [2, 3, 4], [3, 4], [4]]
// Iterator shorter than windowSize — "only-full" yields nothing[...[1, 2].values().windows(3)]// → []
// Iterator shorter than windowSize — "allow-partial" yields what's there[...[1, 2].values().windows(3, "allow-partial")]// → [[1, 2]]