@monstermann/fn
Version:
A utility library for TypeScript.
28 lines (26 loc) • 745 B
JavaScript
import { resolveOffset } from "./internals/offset.js";
import { dfdlT } from "@monstermann/dfdl";
import { cloneArray } from "@monstermann/remmi";
//#region src/array/removeAt.ts
/**
* `removeAt(target, idx)`
*
* 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 original array unchanged.
*
* ```ts
* removeAt([1, 2, 3, 4], 1); // [1, 3, 4]
* ```
*
* ```ts
* pipe([1, 2, 3, 4], removeAt(1)); // [1, 3, 4]
* ```
*/
const removeAt = dfdlT((target, idx) => {
const offset = resolveOffset(target, idx);
if (offset < 0) return target;
target = cloneArray(target);
target.splice(offset, 1);
return target;
}, 2);
//#endregion
export { removeAt };