UNPKG

remeda

Version:

A utility library for JavaScript and Typescript.

1 lines 3.32 kB
{"version":3,"file":"splice.cjs","names":["purryOn"],"sources":["../src/splice.ts"],"sourcesContent":["import { purryOn } from \"./internal/purryOn\";\nimport type { IterableContainer } from \"./internal/types/IterableContainer\";\n\n/**\n * Removes elements from an array and, optionally, inserts new elements in\n * their place.\n *\n * Related operations:\n * - `drop` - to skip past the first `n` elements.\n * - `dropLast` - to skip past the last `n` elements.\n * - `filter` - to shape the array by *value* rather than by *position*.\n * - `swapIndices` - to swap two elements by their indices.\n * - `take` - to keep only the first `n` elements.\n * - `takeLast` - to keep only the last `n` elements.\n *\n * @param data - The array to splice.\n * @param start - The index from which to start removing elements.\n * @param deleteCount - The number of elements to remove.\n * @param replacement - Elements to insert at the cutoff point.\n * @signature\n * splice(data, start, deleteCount);\n * splice(data, start, deleteCount, replacement);\n * @example\n * splice([1,2,3,4,5,6,7,8], 2, 3); //=> [1,2,6,7,8]\n * splice([1,2,3,4,5,6,7,8], 2, 3, [9, 10]); //=> [1,2,9,10,6,7,8]\n * @dataFirst\n * @category Array\n */\nexport function splice<T extends IterableContainer>(\n data: T,\n start: number,\n deleteCount: number,\n replacement?: readonly T[number][],\n): T[number][];\n\n/**\n * Removes elements from an array and, optionally, inserts new elements in\n * their place.\n *\n * Related operations:\n * - `drop` - to skip past the first `n` elements.\n * - `dropLast` - to skip past the last `n` elements.\n * - `filter` - to shape the array by *value* rather than by *position*.\n * - `swapIndices` - to swap two elements by their indices.\n * - `take` - to keep only the first `n` elements.\n * - `takeLast` - to keep only the last `n` elements.\n *\n * @param start - The index from which to start removing elements.\n * @param deleteCount - The number of elements to remove.\n * @param replacement - Elements to insert at the cutoff point.\n * @signature\n * splice(start, deleteCount)(data);\n * splice(start, deleteCount, replacement)(data);\n * @example\n * pipe([1,2,3,4,5,6,7,8], splice(2, 3)) // => [1,2,6,7,8]\n * pipe([1,2,3,4,5,6,7,8], splice(2, 3, [9, 10])) // => [1,2,9,10,6,7,8]\n * @dataLast\n * @category Array\n */\nexport function splice<T extends IterableContainer>(\n start: number,\n deleteCount: number,\n replacement?: readonly T[number][],\n): (data: T) => T[number][];\n\nexport function splice(...args: readonly unknown[]): unknown {\n return purryOn(($) => typeof $ === \"number\", spliceImplementation, args);\n}\n\nfunction spliceImplementation<T extends IterableContainer>(\n data: T,\n start: number,\n deleteCount: number,\n replacement: readonly T[number][] = [],\n): T[number][] {\n // TODO [>2]: When node 18 reaches end-of-life bump target lib to ES2023+ and use `Array.prototype.toSpliced` here.\n const result = [...data];\n result.splice(start, deleteCount, ...replacement);\n return result;\n}\n"],"mappings":"6GAiEA,SAAgB,EAAO,GAAG,EAAmC,CAC3D,OAAOA,EAAAA,EAAS,GAAM,OAAO,GAAM,SAAU,EAAsB,CAAI,CACzE,CAEA,SAAS,EACP,EACA,EACA,EACA,EAAoC,CAAC,EACxB,CAEb,IAAM,EAAS,CAAC,GAAG,CAAI,EAEvB,OADA,EAAO,OAAO,EAAO,EAAa,GAAG,CAAW,EACzC,CACT"}