VDL Plugin SDK

windowed

Import from @varavel/vdl-plugin-sdk/utils/arrays.

function windowed<T>(
   arr, 
   size, 
   step?, 
   options?
): T[][];

Creates an array of sub-arrays (windows) from the input array, each of the specified size. The windows can overlap depending on the step size provided.

By default, only full windows are included in the result, and any leftover elements that can't form a full window are ignored.

If the partialWindows option is set to true in the options object, the function will also include partial windows at the end of the result. Partial windows are smaller sub-arrays created when there aren't enough elements left in the input array to form a full window.

Type Parameters

Type ParameterDescription
T

Parameters

ParameterTypeDescription
arrreadonly T[]The input array to create windows from.
sizenumberThe size of each window. Must be a positive integer.
step?numberThe step size between the start of each window. Must be a positive integer.
options?WindowedOptionsOptions object to configure the behavior of the function.

Returns

T[][]

An array of windows (sub-arrays) created from the input array.

Throws

If the size or step is not a positive integer.

Examples

windowed([1, 2, 3, 4], 2);
// => [[1, 2], [2, 3], [3, 4]]
windowed([1, 2, 3, 4, 5, 6], 3, 2);
// => [[1, 2, 3], [3, 4, 5]]
windowed([1, 2, 3, 4, 5, 6], 3, 2, { partialWindows: true });
// => [[1, 2, 3], [3, 4, 5], [5, 6]]