0.1.0 • Published 3 years ago
@pacote/windowed v0.1.0
@pacote/windowed
Returns a snapshot array of a window sliding along an array, inspired by
Kotlin's windowed
method.
Installation
yarn add @pacote/windowed
Usage
import { windowed } from '@pacote/windowed'
const array = [1, 2, 3, 4]
windowed(2, array) // => [[1, 2], [2, 3], [3, 4]]
windowed(3, array) // => [[1, 2, 3], [2, 3, 4]]
windowed(2, 2, array) // => [[1, 2], [3, 4]]
windowed<T>(size: number, step: number = 1, collection: T[]): T[][]
windowed()
returns a snapshot array of a window of the provided size
sliding
along the provided array with the provided step
.
Both size and step must be positive and can be greater than the number of elements in this collection.
Arguments
size
: the number of elements to take in each window.step
: the number of elements to move the window forward by on an each step, by default1
.collection
: the array the window slides along.
License
MIT © Luís Rodrigues.
0.1.0
3 years ago