indexOfOrElse
ts
function Array.indexOfOrElse<T, U>(
target: readonly T[],
value: NoInfer<T>,
orElse: (target: readonly NoInfer<T>[]) => U,
): number | UReturns the index of the first occurrence of value in target. If value is not found, calls orElse with the original array.
Example
ts
import { Array } from "@monstermann/array";
Array.indexOfOrElse([1, 2, 3, 2], 2, () => -1); // 1
Array.indexOfOrElse([1, 2, 3], 4, (arr) => arr.length); // 3ts
import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 2],
Array.indexOfOrElse(2, () => -1),
); // 1
pipe(
[1, 2, 3],
Array.indexOfOrElse(4, (arr) => arr.length),
); // 3