@monstermann/fn
Version:
A utility library for TypeScript.
30 lines (28 loc) • 840 B
JavaScript
import { resolveOffset } from "./internals/offset.js";
import { dfdlT } from "@monstermann/dfdl";
import { cloneArray } from "@monstermann/remmi";
//#region src/array/removeAtOr.ts
/**
* `removeAtOr(target, idx, or)`
*
* Removes the element at index `idx` from `target` array. Supports negative indices to count from the end. If the index is out of bounds, returns the fallback value `or`.
*
* ```ts
* removeAtOr([1, 2, 3], 1, []); // [1, 3]
* removeAtOr([1, 2, 3], 5, []); // []
* ```
*
* ```ts
* pipe([1, 2, 3], removeAtOr(1, [])); // [1, 3]
* pipe([1, 2, 3], removeAtOr(5, [])); // []
* ```
*/
const removeAtOr = dfdlT((target, idx, or) => {
const offset = resolveOffset(target, idx);
if (offset < 0) return or;
const result = cloneArray(target);
result.splice(offset, 1);
return result;
}, 3);
//#endregion
export { removeAtOr };