@monstermann/fn
Version:
A utility library for TypeScript.
30 lines (28 loc) • 874 B
JavaScript
import { FnError } from "../function/FnError.js";
import { resolveOffset } from "./internals/offset.js";
import { dfdlT } from "@monstermann/dfdl";
import { cloneArray } from "@monstermann/remmi";
//#region src/array/removeAtOrThrow.ts
/**
* `removeAtOrThrow(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, throws an error.
*
* ```ts
* removeAtOrThrow([1, 2, 3], 1); // [1, 3]
* ```
*
* ```ts
* pipe([1, 2, 3], removeAtOrThrow(1)); // [1, 3]
* ```
*
*/
const removeAtOrThrow = dfdlT((target, idx) => {
const offset = resolveOffset(target, idx);
if (offset < 0) throw new FnError("removeAtOrThrow: Index is out of range.", [target, idx]);
const result = cloneArray(target);
result.splice(offset, 1);
return result;
}, 2);
//#endregion
export { removeAtOrThrow };