@monstermann/fn
Version:
A utility library for TypeScript.
27 lines (25 loc) • 653 B
JavaScript
import { dfdlT } from "@monstermann/dfdl";
import { cloneArray } from "@monstermann/remmi";
//#region src/array/removeLast.ts
/**
* `removeLast(target, value)`
*
* Removes the last occurrence of `value` from `target` array. If the value is not found, returns the original array unchanged.
*
* ```ts
* removeLast([1, 2, 3, 2], 2); // [1, 2, 3]
* ```
*
* ```ts
* pipe([1, 2, 3, 2], removeLast(2)); // [1, 2, 3]
* ```
*/
const removeLast = dfdlT((target, value) => {
const idx = target.lastIndexOf(value);
if (idx < 0) return target;
const result = cloneArray(target);
result.splice(idx, 1);
return result;
}, 2);
//#endregion
export { removeLast };