remeda
Version:
A utility library for JavaScript and Typescript.
1 lines • 2.32 kB
Source Map (JSON)
{"version":3,"file":"splice.cjs","names":["purry"],"sources":["../src/splice.ts"],"sourcesContent":["import { purry } from \"./purry\";\n\n/**\n * Removes elements from an array and, inserts new elements in their place.\n *\n * @param items - 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 - The elements to insert into the array in place of the deleted elements.\n * @signature\n * R.splice(items, start, deleteCount, replacement)\n * @example\n * R.splice([1,2,3,4,5,6,7,8], 2, 3, []); //=> [1,2,6,7,8]\n * R.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>(\n items: ReadonlyArray<T>,\n start: number,\n deleteCount: number,\n replacement: ReadonlyArray<T>,\n): Array<T>;\n\n/**\n * Removes elements from an array and, inserts new elements in their place.\n *\n * @param start - The index from which to start removing elements.\n * @param deleteCount - The number of elements to remove.\n * @param replacement - The elements to insert into the array in place of the deleted elements.\n * @signature\n * R.splice(start, deleteCount, replacement)(items)\n * @example\n * R.pipe([1,2,3,4,5,6,7,8], R.splice(2, 3, [])) // => [1,2,6,7,8]\n * R.pipe([1,2,3,4,5,6,7,8], R.splice(2, 3, [9, 10])) // => [1,2,9,10,6,7,8]\n * @dataLast\n * @category Array\n */\nexport function splice<T>(\n start: number,\n deleteCount: number,\n replacement: ReadonlyArray<T>,\n): (items: ReadonlyArray<T>) => Array<T>;\n\nexport function splice(...args: ReadonlyArray<unknown>): unknown {\n return purry(spliceImplementation, args);\n}\n\nfunction spliceImplementation<T>(\n items: ReadonlyArray<T>,\n start: number,\n deleteCount: number,\n replacement: ReadonlyArray<T>,\n): Array<T> {\n // TODO [>2]: When node 18 reaches end-of-life bump target lib to ES2023+ and use `Array.prototype.toSpliced` here.\n const result = [...items];\n result.splice(start, deleteCount, ...replacement);\n return result;\n}\n"],"mappings":"wCA4CA,SAAgB,EAAO,GAAG,EAAuC,CAC/D,OAAOA,EAAAA,EAAM,EAAsB,EAAK,CAG1C,SAAS,EACP,EACA,EACA,EACA,EACU,CAEV,IAAM,EAAS,CAAC,GAAG,EAAM,CAEzB,OADA,EAAO,OAAO,EAAO,EAAa,GAAG,EAAY,CAC1C"}