MCPcopy Create free account
hub / github.com/kobaltedev/kobalte / addItemToArray

Function addItemToArray

packages/utils/src/array.ts:12–21  ·  view source on GitHub ↗
(
	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 */
12export 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.

Callers 1

registerItemFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected