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