UNPKG

remeda

Version:

A utility library for JavaScript and Typescript.

1 lines 1.62 kB
{"version":3,"file":"splitAt.cjs","names":["purry"],"sources":["../src/splitAt.ts"],"sourcesContent":["import { purry } from \"./purry\";\n\n/**\n * Splits a given array at a given index.\n *\n * @param array - The array to split.\n * @param index - The index to split at.\n * @signature\n * R.splitAt(array, index)\n * @example\n * R.splitAt([1, 2, 3], 1) // => [[1], [2, 3]]\n * R.splitAt([1, 2, 3, 4, 5], -1) // => [[1, 2, 3, 4], [5]]\n * @dataFirst\n * @category Array\n */\nexport function splitAt<T>(array: readonly T[], index: number): [T[], T[]];\n\n/**\n * Splits a given array at a given index.\n *\n * @param index - The index to split at.\n * @signature\n * R.splitAt(index)(array)\n * @example\n * R.splitAt(1)([1, 2, 3]) // => [[1], [2, 3]]\n * R.splitAt(-1)([1, 2, 3, 4, 5]) // => [[1, 2, 3, 4], [5]]\n * @dataLast\n * @category Array\n */\nexport function splitAt<T>(index: number): (array: readonly T[]) => [T[], T[]];\n\nexport function splitAt(...args: readonly unknown[]): unknown {\n return purry(splitAtImplementation, args);\n}\n\nfunction splitAtImplementation<T>(\n array: readonly T[],\n index: number,\n): [T[], T[]] {\n const effectiveIndex = Math.max(\n Math.min(index < 0 ? array.length + index : index, array.length),\n 0,\n );\n return [array.slice(0, effectiveIndex), array.slice(effectiveIndex)];\n}\n"],"mappings":"wCA+BA,SAAgB,EAAQ,GAAG,EAAmC,CAC5D,OAAOA,EAAAA,EAAM,EAAuB,EAAK,CAG3C,SAAS,EACP,EACA,EACY,CACZ,IAAM,EAAiB,KAAK,IAC1B,KAAK,IAAI,EAAQ,EAAI,EAAM,OAAS,EAAQ,EAAO,EAAM,OAAO,CAChE,EACD,CACD,MAAO,CAAC,EAAM,MAAM,EAAG,EAAe,CAAE,EAAM,MAAM,EAAe,CAAC"}