UNPKG

remeda

Version:

A utility library for JavaScript and Typescript.

1 lines 4.65 kB
{"version":3,"file":"sortedIndexBy.cjs","names":["purry","binarySearchCutoffIndex"],"sources":["../src/sortedIndexBy.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 using a value function; so that\n * `splice(sortedIndex, 0, item)` would result in maintaining the arrays sort-\n * ness. The array can contain duplicates.\n * If the item already exists in the array the index would be of the *first*\n * occurrence of the item.\n *\n * Runs in O(logN) time.\n *\n * See also:\n * * `findIndex` - scans a possibly unsorted array in-order (linear search).\n * * `sortedIndex` - like this function, but doesn't take a callbackfn.\n * * `sortedLastIndexBy` - like this function, but finds the last suitable index.\n * * `sortedLastIndex` - like `sortedIndex`, but finds the last suitable index.\n * * `rankBy` - scans a possibly unsorted array in-order, returning the index based on a sorting criteria.\n *\n * @param data - The (ascending) sorted array.\n * @param item - The item to insert.\n * @param valueFunction - All comparisons would be performed on the result of\n * calling this function on each compared item. Preferably this function should\n * return a `number` or `string`. This function should be the same as the one\n * provided to sortBy to sort the array. The function is called exactly once on\n * each items that is compared against in the array, and once at the beginning\n * on `item`. When called on `item` the `index` argument is `undefined`.\n * @returns Insertion index (In the range 0..data.length).\n * @signature\n * R.sortedIndexBy(data, item, valueFunction)\n * @example\n * R.sortedIndexBy([{age:20},{age:22}],{age:21},prop('age')) // => 1\n * @dataFirst\n * @category Array\n */\nexport function sortedIndexBy<T>(\n data: ReadonlyArray<T>,\n item: T,\n valueFunction: (\n item: T,\n index: number | undefined,\n data: ReadonlyArray<T>,\n ) => NonNullable<unknown>,\n): number;\n\n/**\n * Find the insertion position (index) of an item in an array with items sorted\n * in ascending order using a value function; so that\n * `splice(sortedIndex, 0, item)` would result in maintaining the arrays sort-\n * ness. The array can contain duplicates.\n * If the item already exists in the array the index would be of the *first*\n * occurrence of the item.\n *\n * Runs in O(logN) time.\n *\n * See also:\n * * `findIndex` - scans a possibly unsorted array in-order (linear search).\n * * `sortedIndex` - like this function, but doesn't take a callbackfn.\n * * `sortedLastIndexBy` - like this function, but finds the last suitable index.\n * * `sortedLastIndex` - like `sortedIndex`, but finds the last suitable index.\n * * `rankBy` - scans a possibly unsorted array in-order, returning the index based on a sorting criteria.\n *\n * @param item - The item to insert.\n * @param valueFunction - All comparisons would be performed on the result of\n * calling this function on each compared item. Preferably this function should\n * return a `number` or `string`. This function should be the same as the one\n * provided to sortBy to sort the array. The function is called exactly once on\n * each items that is compared against in the array, and once at the beginning\n * on `item`. When called on `item` the `index` argument is `undefined`.\n * @signature\n * R.sortedIndexBy(data, item, valueFunction)\n * @example\n * R.sortedIndexBy([{age:20},{age:22}],{age:21},prop('age')) // => 1\n * @dataLast\n * @category Array\n */\nexport function sortedIndexBy<T>(\n item: T,\n valueFunction: (\n item: T,\n index: number | undefined,\n data: ReadonlyArray<T>,\n ) => NonNullable<unknown>,\n): (data: ReadonlyArray<T>) => number;\n\nexport function sortedIndexBy(...args: ReadonlyArray<unknown>): unknown {\n return purry(sortedIndexByImplementation, args);\n}\n\nfunction sortedIndexByImplementation<T>(\n data: ReadonlyArray<T>,\n item: T,\n valueFunction: (\n item: T,\n index: number | undefined,\n data: ReadonlyArray<T>,\n ) => NonNullable<unknown>,\n): number {\n const value = valueFunction(item, undefined /* index */, data);\n return binarySearchCutoffIndex(\n data,\n (pivot, index) => valueFunction(pivot, index, data) < value,\n );\n}\n"],"mappings":"4FAsFA,SAAgB,EAAc,GAAG,EAAuC,CACtE,OAAOA,EAAAA,EAAM,EAA6B,EAAK,CAGjD,SAAS,EACP,EACA,EACA,EAKQ,CACR,IAAM,EAAQ,EAAc,EAAM,IAAA,GAAuB,EAAK,CAC9D,OAAOC,EAAAA,EACL,GACC,EAAO,IAAU,EAAc,EAAO,EAAO,EAAK,CAAG,EACvD"}