Function
addItemToArray
(
array: T,
item: T[number],
index = -1,
)
Source from the content-addressed store, hash-verified
| 10 | * Immutably adds an item at the given index to an array. |
| 11 | */ |
| 12 | export function addItemToArray<T extends any[]>( |
| 13 | array: T, |
| 14 | item: T[number], |
| 15 | index = -1, |
| 16 | ) { |
| 17 | if (!(index in array)) { |
| 18 | return [...array, item] as T; |
| 19 | } |
| 20 | return [...array.slice(0, index), item, ...array.slice(index)] as T; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Immutably removes an item from an array. |
Tested by
no test coverage detected