UNPKG

@writetome51/array-get-adjacent-at

Version:

Returns chosen number of adjacent items in array beginning at chosen index

15 lines (10 loc) 554 B
import {getArrFilled} from '@writetome51/get-arr-filled'; import {validateAdjacentItemsOperationArgs} from '@writetome51/validate-adjacent-items-operation-args'; // Intended as a replacement of Array.prototype.slice() . It strictly validates args. // `startingIndex` can be negative or positive. export const getAdjacentAt = (startingIndex, howMany, array) => { validateAdjacentItemsOperationArgs(startingIndex, howMany, array); if (startingIndex < 0) startingIndex += array.length; return getArrFilled(howMany, () => array[startingIndex++]); }