Skip to main content

latest
Works with
It is unknown whether this package works with Browsers, Deno, Node.js, Cloudflare Workers, Bun
It is unknown whether this package works with Browsers
It is unknown whether this package works with Deno
It is unknown whether this package works with Node.js
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score70%
Downloads1/wk
Published2 years ago (0.215.0)
function slidingWindows
slidingWindows<T>(
array: readonly T[],
size: number,
unnamed 2?: { step?: number; partial?: boolean; }
): T[][]

Generates sliding views of the given array of the given size and returns a new array containing all of them.

If step is set, each window will start that many elements after the last window's start. (Default: 1)

If partial is set, windows will be generated for the last elements of the collection, resulting in some undefined values if size is greater than 1.

Examples

Example 1

import { slidingWindows } from "@std/collections/sliding_windows";
import { assertEquals } from "@std/assert/assert_equals";
const numbers = [1, 2, 3, 4, 5];

const windows = slidingWindows(numbers, 3);
assertEquals(windows, [
  [1, 2, 3],
  [2, 3, 4],
  [3, 4, 5],
]);

const windowsWithStep = slidingWindows(numbers, 3, { step: 2 });
assertEquals(windowsWithStep, [
  [1, 2, 3],
  [3, 4, 5],
]);

const windowsWithPartial = slidingWindows(numbers, 3, { partial: true });
assertEquals(windowsWithPartial, [
  [1, 2, 3],
  [2, 3, 4],
  [3, 4, 5],
  [4, 5],
  [5],
]);

Type Parameters

Parameters

array: readonly T[]
optional
unnamed 2: { step?: number; partial?: boolean; }

Return Type

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/collections

Import symbol

import { slidingWindows } from "@std/collections";
or

Import directly with a jsr specifier

import { slidingWindows } from "jsr:@std/collections";