| 1 | /**
|
|---|
| 2 | * Options for the windowed function.
|
|---|
| 3 | *
|
|---|
| 4 | * @interface WindowedOptions
|
|---|
| 5 | * @property {boolean} [partialWindows=false] - Whether to include partial windows at the end of the array.
|
|---|
| 6 | */
|
|---|
| 7 | interface WindowedOptions {
|
|---|
| 8 | /**
|
|---|
| 9 | * Whether to include partial windows at the end of the array.
|
|---|
| 10 | *
|
|---|
| 11 | * By default, `windowed` only includes full windows in the result,
|
|---|
| 12 | * ignoring any leftover elements that can't form a full window.
|
|---|
| 13 | *
|
|---|
| 14 | * If `partialWindows` is true, the function will also include these smaller, partial windows at the end of the result.
|
|---|
| 15 | */
|
|---|
| 16 | partialWindows?: boolean;
|
|---|
| 17 | }
|
|---|
| 18 | /**
|
|---|
| 19 | * Creates an array of sub-arrays (windows) from the input array, each of the specified size.
|
|---|
| 20 | * The windows can overlap depending on the step size provided.
|
|---|
| 21 | *
|
|---|
| 22 | * By default, only full windows are included in the result, and any leftover elements that can't form a full window are ignored.
|
|---|
| 23 | *
|
|---|
| 24 | * 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.
|
|---|
| 25 | * Partial windows are smaller sub-arrays created when there aren't enough elements left in the input array to form a full window.
|
|---|
| 26 | *
|
|---|
| 27 | * @template T
|
|---|
| 28 | * @param {readonly T[]} arr - The input array to create windows from.
|
|---|
| 29 | * @param {number} size - The size of each window. Must be a positive integer.
|
|---|
| 30 | * @param {number} [step=1] - The step size between the start of each window. Must be a positive integer.
|
|---|
| 31 | * @param {WindowedOptions} [options={}] - Options object to configure the behavior of the function.
|
|---|
| 32 | * @param {boolean} [options.partialWindows=false] - Whether to include partial windows at the end of the array.
|
|---|
| 33 | * @returns {T[][]} An array of windows (sub-arrays) created from the input array.
|
|---|
| 34 | * @throws {Error} If the size or step is not a positive integer.
|
|---|
| 35 | *
|
|---|
| 36 | * @example
|
|---|
| 37 | * windowed([1, 2, 3, 4], 2);
|
|---|
| 38 | * // => [[1, 2], [2, 3], [3, 4]]
|
|---|
| 39 | *
|
|---|
| 40 | * @example
|
|---|
| 41 | * windowed([1, 2, 3, 4, 5, 6], 3, 2);
|
|---|
| 42 | * // => [[1, 2, 3], [3, 4, 5]]
|
|---|
| 43 | *
|
|---|
| 44 | * @example
|
|---|
| 45 | * windowed([1, 2, 3, 4, 5, 6], 3, 2, { partialWindows: true });
|
|---|
| 46 | * // => [[1, 2, 3], [3, 4, 5], [5, 6]]
|
|---|
| 47 | */
|
|---|
| 48 | declare function windowed<T>(arr: readonly T[], size: number, step?: number, { partialWindows }?: WindowedOptions): T[][];
|
|---|
| 49 |
|
|---|
| 50 | export { type WindowedOptions, windowed };
|
|---|