@monstermann/fn
Version:
A utility library for TypeScript.
31 lines (29 loc) • 992 B
JavaScript
import { dfdlT } from "@monstermann/dfdl";
import { markAsMutable } from "@monstermann/remmi";
//#region src/array/insertAllAt.ts
/**
* `insertAllAt(array, index, values)`
*
* Inserts all elements from `values` at the specified `index` in `array`, returning a new array with the inserted elements. Supports iterables for the `values` parameter.
*
* ```ts
* insertAllAt([1, 2, 3], 1, [10, 20]); // [1, 10, 20, 2, 3]
* ```
*
* ```ts
* pipe([1, 2, 3], insertAllAt(1, [10, 20])); // [1, 10, 20, 2, 3]
* ```
*/
const insertAllAt = dfdlT((target, idx, values) => {
if (idx < 0 || idx > target.length) return target;
const vs = Array.from(values);
const bLen = vs.length;
if (bLen === 0) return target;
const aLen = target.length;
const result = Array.from({ length: aLen + bLen });
for (let i = 0; i < aLen; i++) result[i >= idx ? i + bLen : i] = target[i];
for (let i = 0; i < bLen; i++) result[idx + i] = vs[i];
return markAsMutable(result);
}, 3);
//#endregion
export { insertAllAt };