@monstermann/fn
Version:
A utility library for TypeScript.
31 lines (29 loc) • 951 B
JavaScript
import { FnError } from "../function/FnError.js";
import { insertAllAt } from "./insertAllAt.js";
import { dfdlT } from "@monstermann/dfdl";
//#region src/array/insertAllAtOrThrow.ts
/**
* `insertAllAtOrThrow(target, idx, values)`
*
* Inserts all `values` at the specified `idx` in `target`. If the index is out of bounds, throws an error. Supports iterables.
*
* ```ts
* insertAllAtOrThrow([1, 2, 3], 1, [8, 9]); // [1, 8, 9, 2, 3]
* insertAllAtOrThrow([1, 2, 3], 5, [8, 9]); // throws FnError
* ```
*
* ```ts
* pipe([1, 2, 3], insertAllAtOrThrow(1, [8, 9])); // [1, 8, 9, 2, 3]
* pipe([1, 2, 3], insertAllAtOrThrow(5, [8, 9])); // throws FnError
* ```
*/
const insertAllAtOrThrow = dfdlT((target, idx, values) => {
if (idx < 0 || idx > target.length) throw new FnError("Array.insertAllAtOrThrow: Index is out of range.", [
target,
idx,
values
]);
return insertAllAt(target, idx, values);
}, 3);
//#endregion
export { insertAllAtOrThrow };