@writetome51/array-replace-adjacent-at

Replaces adjacent items in the array with same number of new adjacent items

Usage no npm install needed!

<script type="module">
  import writetome51ArrayReplaceAdjacentAt from 'https://cdn.skypack.dev/@writetome51/array-replace-adjacent-at';
</script>

README

replaceAdjacentAt<T>(
      startingIndex: number,
      newValues: T[],
      array: T[]
): void

Replaces adjacent items, beginning at startingIndex, with the same number
of newValues, in array.
index can be negative or positive.

Examples:

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

replaceAdjacentAt(0, [20], arr);  
// arr is now  [ 20, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

replaceAdjacentAt(0, [30, 31, 32, 33], arr);  
// arr is now  [ 30, 31, 32, 33, 5, 6, 7, 8, 9, 10 ]

replaceAdjacentAt(-3, [1, 2, 3], arr);
// arr is now  [ 30, 31, 32, 33, 5, 6, 7, 1, 2, 3 ]

// This works because the second arg has length greater than zero:
replaceAdjacentAt(-3, 'aaa', arr);
// arr is now  [ 30, 31, 32, 33, 5, 6, 7, 'a', 'a', 'a' ]

// This errors because the second arg has length of zero:
replaceAdjacentAt(0, [], arr);
// Error: 'Input must be integer greater than zero'

// This errors because arr only has 2 items left at index 8:
replaceAdjacentAt(8, ['a', 'b', 'c'], arr);
// Error: 'the array does not have enough items to fulfill your request'

Installation

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

Loading

import { replaceAdjacentAt } from '@writetome51/array-replace-adjacent-at';