Skip to content

Function returns chosen number of adjacent items in array beginning at chosen index

License

Notifications You must be signed in to change notification settings

writetome51/array-get-adjacent-at

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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';

About

Function returns chosen number of adjacent items in array beginning at chosen index

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published