Skip to content

insertAtOrElse

Array.insertAtOrElse(array, index, value, callback)

Inserts value at the specified index in array, returning a new array with the inserted element, or the result of calling callback with the array if the index is out of bounds.

Example

ts
import { Array } from "@monstermann/array";

Array.insertAtOrElse([1, 2, 3], 10, 99, (arr) => arr.length); // 3
ts
import { Array } from "@monstermann/array";

pipe(
    [1, 2, 3],
    Array.insertAtOrElse(10, 99, (arr) => arr.length),
); // 3