@writetome51/array-get-adjacent-at
TypeScript icon, indicating that this package has built-in type declarations

3.0.3 • Public • Published

getAdjacentAt<T>(
       startingIndex: number,
       howMany: number,
       array: T[]
): T[]

Beginning at startingIndex, returns howMany adjacent items from array.
Does not modify array. startingIndex can be negative or positive.
Intended as a replacement of Array.prototype.slice() . It strictly validates args.

Examples

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
getAdjacentAt(0, 3, arr);
// --> [1,2,3]

getAdjacentAt(2, 5, arr);
// --> [3,4,5,6,7]

// You can pass a negative index:
getAdjacentAt(-3, 2, arr);
// --> [8, 9]

getAdjacentAt(-2, 3, arr); // Requesting one too may items
//Error: 'the array does not have enough items to fulfill your request'

Installation

npm i @writetome51/array-get-adjacent-at

Loading

import { getAdjacentAt } from '@writetome51/array-get-adjacent-at';

/@writetome51/array-get-adjacent-at/

    Package Sidebar

    Install

    npm i @writetome51/array-get-adjacent-at

    Weekly Downloads

    14

    Version

    3.0.3

    License

    MIT

    Unpacked Size

    3.63 kB

    Total Files

    5

    Last publish

    Collaborators

    • writetome51