UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

25 lines (23 loc) 526 B
import { dfdlT } from "@monstermann/dfdl"; //#region src/array/lastOrElse.ts /** * `lastOrElse(array, callback)` * * Returns the last element of `array`, or the result of calling `callback` with the array if the array is empty. * * ```ts * lastOrElse([1, 2, 3, 4], (arr) => arr.length); // 4 * ``` * * ```ts * pipe( * [1, 2, 3, 4], * lastOrElse((arr) => arr.length), * ); // 4 * ``` */ const lastOrElse = dfdlT((target, orElse) => { return target.at(-1) ?? orElse(target); }, 2); //#endregion export { lastOrElse };