UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

32 lines (30 loc) 924 B
import { insertAllAt } from "./insertAllAt.js"; import { dfdlT } from "@monstermann/dfdl"; //#region src/array/insertAllAtOrElse.ts /** * `insertAllAtOrElse(target, idx, values, orElse)` * * Inserts all `values` at the specified `idx` in `target`. If the index is out of bounds, calls `orElse` with the original array. Supports iterables. * * ```ts * insertAllAtOrElse([1, 2, 3], 1, [8, 9], () => []); // [1, 8, 9, 2, 3] * insertAllAtOrElse([1, 2, 3], 5, [8, 9], (arr) => arr); // [1, 2, 3] * ``` * * ```ts * pipe( * [1, 2, 3], * insertAllAtOrElse(1, [8, 9], () => []), * ); // [1, 8, 9, 2, 3] * pipe( * [1, 2, 3], * insertAllAtOrElse(5, [8, 9], (arr) => arr), * ); // [1, 2, 3] * ``` */ const insertAllAtOrElse = dfdlT((target, idx, values, orElse) => { if (idx < 0 || idx > target.length) return orElse(target); return insertAllAt(target, idx, values); }, 4); //#endregion export { insertAllAtOrElse };