Skip to content

windowed

windowed<T>(iterable, size, step?): IterableIterator<T[]>

Defined in: packages/core/src/iterable/windowed.ts:22

Yields every overlapping window of size consecutive elements.

Not chunk: windows overlap, chunks do not. windowed([1, 2, 3], 2) yields [1, 2] then [2, 3], where chunk([1, 2, 3], 2) yields [1, 2] then [3]. Passing step equal to size makes the two agree, except that a short trailing group is never yielded here.

An input shorter than size yields nothing, and so does the tail left over when the length is not reached again — a window is only ever yielded at full width.

Time complexity: O(n), allocating one array per window.

T

Iterable<T>

The input iterable.

number

How many elements per window. Must be a positive integer.

number = 1

How far to advance between windows. Must be a positive integer; a value above size skips elements entirely.

IterableIterator<T[]>

An iterator over the windows.

If size or step is not a positive integer.