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