@monstermann/fn
Version:
A utility library for TypeScript.
30 lines (28 loc) • 786 B
JavaScript
import { dfdlT } from "@monstermann/dfdl";
import { cloneArray } from "@monstermann/remmi";
//#region src/array/findRemoveOr.ts
/**
* `findRemoveOr(array, predicate, fallback)`
*
* Finds the first 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
* findRemoveOr([1, 2, 3, 4], (x) => x > 10, []); // []
* ```
*
* ```ts
* pipe(
* [1, 2, 3, 4],
* findRemoveOr((x) => x > 10, []),
* ); // []
* ```
*/
const findRemoveOr = dfdlT((target, predicate, or) => {
const idx = target.findIndex(predicate);
if (idx === -1) return or;
const result = cloneArray(target);
result.splice(idx, 1);
return result;
}, 3);
//#endregion
export { findRemoveOr };