remeda
Version:
A utility library for JavaScript and Typescript.
1 lines • 4.33 kB
Source Map (JSON)
{"version":3,"file":"sortedIndexWith.cjs","names":["purry","binarySearchCutoffIndex"],"sources":["../src/sortedIndexWith.ts"],"sourcesContent":["import { purry } from \"./purry\";\nimport { binarySearchCutoffIndex } from \"./internal/binarySearchCutoffIndex\";\n\n/**\n * Performs a **binary search** for the index of the item at which the predicate\n * stops returning `true`. This function assumes that the array is \"sorted\" in\n * regards to the predicate, meaning that running the predicate as a mapper on\n * it would result in an array `[...true[], ...false[]]`.\n * This stricter requirement from the predicate provides us 2 benefits over\n * `findIndex` which does a similar thing:\n * 1. It would run at O(logN) time instead of O(N) time.\n * 2. It always returns a value (it would return `data.length` if the\n * predicate returns `true` for all items).\n *\n * This function is the basis for all other sortedIndex functions which search\n * for a specific item in a sorted array, and it could be used to perform\n * similar efficient searches.\n * * `sortedIndex` - scans a sorted array with a binary search, find the first suitable index.\n * * `sortedIndexBy` - like `sortedIndex`, but assumes sorting is based on a callbackfn.\n * * `sortedLastIndex` - scans a sorted array with a binary search, finding the last suitable index.\n * * `sortedLastIndexBy` - like `sortedLastIndex`, but assumes sorting is based on a callbackfn.\n *\n * See also:\n * * `findIndex` - scans a possibly unsorted array in-order (linear search).\n * * `rankBy` - scans a possibly unsorted array in-order, returning the index based on a sorting criteria.\n *\n * @param data - Array, \"sorted\" by `predicate`.\n * @param predicate - A predicate which also defines the array's order.\n * @returns Index (In the range 0..data.length).\n * @signature\n * R.sortedIndexWith(data, predicate)\n * @example\n * R.sortedIndexWith(['a','ab','abc'], (item) => item.length < 2) // => 1\n * @dataFirst\n * @category Array\n * @see findIndex, sortedIndex, sortedIndexBy, sortedLastIndex, sortedLastIndexBy\n */\nexport function sortedIndexWith<T>(\n data: ReadonlyArray<T>,\n predicate: (value: T, index: number, data: ReadonlyArray<T>) => boolean,\n): number;\n\n/**\n * Performs a **binary search** for the index of the item at which the predicate\n * stops returning `true`. This function assumes that the array is \"sorted\" in\n * regards to the predicate, meaning that running the predicate as a mapper on\n * it would result in an array `[...true[], ...false[]]`.\n * This stricter requirement from the predicate provides us 2 benefits over\n * `findIndex` which does a similar thing:\n * 1. It would run at O(logN) time instead of O(N) time.\n * 2. It always returns a value (it would return `data.length` if the\n * predicate returns `true` for all items).\n *\n * This function is the basis for all other sortedIndex functions which search\n * for a specific item in a sorted array, and it could be used to perform\n * similar efficient searches.\n * * `sortedIndex` - scans a sorted array with a binary search, find the first suitable index.\n * * `sortedIndexBy` - like `sortedIndex`, but assumes sorting is based on a callbackfn.\n * * `sortedLastIndex` - scans a sorted array with a binary search, finding the last suitable index.\n * * `sortedLastIndexBy` - like `sortedLastIndex`, but assumes sorting is based on a callbackfn.\n *\n * See also:\n * * `findIndex` - scans a possibly unsorted array in-order (linear search).\n * * `rankBy` - scans a possibly unsorted array in-order, returning the index based on a sorting criteria.\n *\n * @param predicate - A predicate which also defines the array's order.\n * @returns Index (In the range 0..data.length).\n * @signature\n * R.sortedIndexWith(predicate)(data)\n * @example\n * R.pipe(['a','ab','abc'], R.sortedIndexWith((item) => item.length < 2)) // => 1\n * @dataLast\n * @category Array\n */\nexport function sortedIndexWith<T>(\n predicate: (value: T, index: number, data: ReadonlyArray<T>) => boolean,\n): (data: ReadonlyArray<T>) => number;\n\nexport function sortedIndexWith(...args: ReadonlyArray<unknown>): unknown {\n return purry(binarySearchCutoffIndex, args);\n}\n"],"mappings":"4FA8EA,SAAgB,EAAgB,GAAG,EAAuC,CACxE,OAAOA,EAAAA,EAAMC,EAAAA,EAAyB,EAAK"}