@monstermann/fn
Version:
A utility library for TypeScript.
30 lines (28 loc) • 813 B
JavaScript
import { dfdlT } from "@monstermann/dfdl";
import { cloneArray } from "@monstermann/remmi";
//#region src/array/findRemoveLastOr.ts
/**
* `findRemoveLastOr(array, predicate, fallback)`
*
* Finds the last element in `array` that satisfies the provided `predicate` function and removes it, returning a new array without the removed element, or `fallback` if no element is found.
*
* ```ts
* findRemoveLastOr([1, 2, 3, 4], (x) => x > 10, []); // []
* ```
*
* ```ts
* pipe(
* [1, 2, 3, 4],
* findRemoveLastOr((x) => x > 10, []),
* ); // []
* ```
*/
const findRemoveLastOr = dfdlT((target, predicate, or) => {
const idx = target.findLastIndex(predicate);
if (idx === -1) return or;
const result = cloneArray(target);
result.splice(idx, 1);
return result;
}, 3);
//#endregion
export { findRemoveLastOr };