@monstermann/fn
Version:
A utility library for TypeScript.
26 lines (24 loc) • 766 B
JavaScript
import { insertAllAt } from "./insertAllAt.js";
import { dfdlT } from "@monstermann/dfdl";
//#region src/array/insertAllAtOr.ts
/**
* `insertAllAtOr(target, idx, values, or)`
*
* Inserts all `values` at the specified `idx` in `target`. If the index is out of bounds, returns `or`. Supports iterables.
*
* ```ts
* insertAllAtOr([1, 2, 3], 1, [8, 9], []); // [1, 8, 9, 2, 3]
* insertAllAtOr([1, 2, 3], 5, [8, 9], []); // []
* ```
*
* ```ts
* pipe([1, 2, 3], insertAllAtOr(1, [8, 9], [])); // [1, 8, 9, 2, 3]
* pipe([1, 2, 3], insertAllAtOr(5, [8, 9], [])); // []
* ```
*/
const insertAllAtOr = dfdlT((target, idx, values, or) => {
if (idx < 0 || idx > target.length) return or;
return insertAllAt(target, idx, values);
}, 4);
//#endregion
export { insertAllAtOr };