Skip to content

firstOrElse

ts
function Array.firstOrElse<T, U>(
    target: readonly T[],
    orElse: (target: readonly NoInfer<T>[]) => U,
): Exclude<T, null | undefined> | U

Returns the first element of array, or the result of calling callback with the array if the array is empty.

Example

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

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

pipe(
    [1, 2, 3, 4],
    Array.firstOrElse((arr) => arr.length),
); // 1