remeda
Version:
A utility library for JavaScript and Typescript.
1 lines • 2.06 kB
Source Map (JSON)
{"version":3,"file":"sort.cjs","names":["purry"],"sources":["../src/sort.ts"],"sourcesContent":["import type { IterableContainer } from \"./internal/types/IterableContainer\";\nimport type { ReorderedArray } from \"./internal/types/ReorderedArray\";\nimport { purry } from \"./purry\";\n\n/**\n * Sorts an array. The comparator function should accept two values at a time\n * and return a negative number if the first value is smaller, a positive number\n * if it's larger, and zero if they are equal. Sorting is based on a native\n * `sort` function.\n *\n * @param items - The array to sort.\n * @param cmp - The comparator function.\n * @signature\n * R.sort(items, cmp)\n * @example\n * R.sort([4, 2, 7, 5], (a, b) => a - b); // => [2, 4, 5, 7]\n * @dataFirst\n * @category Array\n */\nexport function sort<T extends IterableContainer>(\n items: T,\n cmp: (a: T[number], b: T[number]) => number,\n): ReorderedArray<T>;\n\n/**\n * Sorts an array. The comparator function should accept two values at a time\n * and return a negative number if the first value is smaller, a positive number\n * if it's larger, and zero if they are equal. Sorting is based on a native\n * `sort` function.\n *\n * @param cmp - The comparator function.\n * @signature\n * R.sort(cmp)(items)\n * @example\n * R.pipe([4, 2, 7, 5], R.sort((a, b) => a - b)) // => [2, 4, 5, 7]\n * @dataLast\n * @category Array\n */\nexport function sort<T extends IterableContainer>(\n cmp: (a: T[number], b: T[number]) => number,\n): (items: T) => ReorderedArray<T>;\n\nexport function sort(...args: ReadonlyArray<unknown>): unknown {\n return purry(sortImplementation, args);\n}\n\nfunction sortImplementation<T extends IterableContainer>(\n items: T,\n cmp: (a: T[number], b: T[number]) => number,\n): ReorderedArray<T> {\n const ret = [...items];\n ret.sort(cmp);\n return ret as ReorderedArray<T>;\n}\n"],"mappings":"wCA0CA,SAAgB,EAAK,GAAG,EAAuC,CAC7D,OAAOA,EAAAA,EAAM,EAAoB,EAAK,CAGxC,SAAS,EACP,EACA,EACmB,CACnB,IAAM,EAAM,CAAC,GAAG,EAAM,CAEtB,OADA,EAAI,KAAK,EAAI,CACN"}