项目作者: writetome51

项目描述 :
Function removes and returns adjacent array items beginning at specified index
高级语言: JavaScript
项目地址: git://github.com/writetome51/array-get-and-remove-adjacent-at.git
创建时间: 2018-11-05T23:08:03Z
项目社区:https://github.com/writetome51/array-get-and-remove-adjacent-at

开源协议:MIT License

下载


getAndRemoveAdjacentAt\(
startingIndex: number,
howMany: number,
array: T[]
): T[]

Beginning at startingIndex, it removes and returns howMany adjacent items from array.
startingIndex can be positive or negative.
Intended as a replacement for Array.prototype.splice() when removing items from array.
This does strict validation for all 3 parameters.

Examples

  1. let arr = [1,2,3,4,5,6];
  2. getAndRemoveAdjacentAt(1, 3, arr);
  3. // --> [2,3,4]
  4. // arr is now [1,5,6]
  5. arr = [1, 2, 3, 4, 5, 6, 7, 8];
  6. getAndRemoveAdjacentAt(-4, 2, arr);
  7. // --> [5,6]
  8. // arr is now [1,2,3,4,7,8].
  9. arr = [1, 2, 3, 4, 5, 6];
  10. getAndRemoveAdjacentAt(-2, 3, arr); // requesting 1 too many items.
  11. // Error: 'the array does not have enough items to fulfill your request'
  12. getAndRemoveAdjacentAt(0, 1, []); // index 0 doesn't exist on empty array.
  13. // Error: 'The entered index is not valid. Whether positive or negative,
  14. // it exceeds the index range of the array.'

Installation

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

Loading

  1. import {getAndRemoveAdjacentAt}
  2. from '@writetome51/array-get-and-remove-adjacent-at';