UNPKG

remeda

Version:

A utility library for JavaScript and Typescript.

1 lines 2.58 kB
{"version":3,"file":"sortedLastIndex.cjs","names":["purry","binarySearchCutoffIndex"],"sources":["../src/sortedLastIndex.ts"],"sourcesContent":["import { purry } from \"./purry\";\nimport { binarySearchCutoffIndex } from \"./internal/binarySearchCutoffIndex\";\n\n/**\n * Find the insertion position (index) of an item in an array with items sorted\n * in ascending order; so that `splice(sortedIndex, 0, item)` would result in\n * maintaining the array's sort-ness. The array can contain duplicates.\n * If the item already exists in the array the index would be of the *last*\n * occurrence of the item.\n *\n * Runs in O(logN) time.\n *\n * @param data - The (ascending) sorted array.\n * @param item - The item to insert.\n * @returns Insertion index (In the range 0..data.length).\n * @signature\n * R.sortedLastIndex(data, item)\n * @example\n * R.sortedLastIndex(['a','a','b','c','c'], 'c') // => 5\n * @dataFirst\n * @category Array\n * @see sortedIndex, sortedIndexBy, sortedIndexWith, sortedLastIndexBy\n */\nexport function sortedLastIndex<T>(data: ReadonlyArray<T>, item: T): number;\n\n/**\n * Find the insertion position (index) of an item in an array with items sorted\n * in ascending order; so that `splice(sortedIndex, 0, item)` would result in\n * maintaining the array's sort-ness. The array can contain duplicates.\n * If the item already exists in the array the index would be of the *last*\n * occurrence of the item.\n *\n * Runs in O(logN) time.\n *\n * @param item - The item to insert.\n * @returns Insertion index (In the range 0..data.length).\n * @signature\n * R.sortedLastIndex(item)(data)\n * @example\n * R.pipe(['a','a','b','c','c'], sortedLastIndex('c')) // => 5\n * @dataLast\n * @category Array\n * @see sortedIndex, sortedIndexBy, sortedIndexWith, sortedLastIndexBy\n */\nexport function sortedLastIndex<T>(item: T): (data: ReadonlyArray<T>) => number;\n\nexport function sortedLastIndex(...args: ReadonlyArray<unknown>): unknown {\n return purry(sortedLastIndexImplementation, args);\n}\n\nconst sortedLastIndexImplementation = <T>(\n array: ReadonlyArray<T>,\n item: T,\n): number =>\n binarySearchCutoffIndex(\n array,\n // The only difference between the regular implementation and the \"last\"\n // variation is that we consider the pivot with equality too, so that we\n // skip all equal values in addition to the lower ones.\n (pivot) => pivot <= item,\n );\n"],"mappings":"4FA8CA,SAAgB,EAAgB,GAAG,EAAuC,CACxE,OAAOA,EAAAA,EAAM,EAA+B,EAAK,CAGnD,MAAM,GACJ,EACA,IAEAC,EAAAA,EACE,EAIC,GAAU,GAAS,EACrB"}