@monstermann/fn
Version:
A utility library for TypeScript.
26 lines (24 loc) • 623 B
JavaScript
import { dfdlT } from "@monstermann/dfdl";
import { cloneArray } from "@monstermann/remmi";
//#region src/array/insertAt.ts
/**
* `insertAt(array, index, value)`
*
* Inserts `value` at the specified `index` in `array`, returning a new array with the inserted element.
*
* ```ts
* insertAt([1, 2, 3], 1, 10); // [1, 10, 2, 3]
* ```
*
* ```ts
* pipe([1, 2, 3], insertAt(1, 10)); // [1, 10, 2, 3]
* ```
*/
const insertAt = dfdlT((target, idx, value) => {
if (idx < 0 || idx > target.length) return target;
target = cloneArray(target);
target.splice(idx, 0, value);
return target;
}, 3);
//#endregion
export { insertAt };