UNPKG

remeda

Version:

A utility library for JavaScript and Typescript.

72 lines 3.89 kB
//#region src/sortedIndexWith.d.ts /** * Performs a **binary search** for the index of the item at which the predicate * stops returning `true`. This function assumes that the array is "sorted" in * regards to the predicate, meaning that running the predicate as a mapper on * it would result in an array `[...true[], ...false[]]`. * This stricter requirement from the predicate provides us 2 benefits over * `findIndex` which does a similar thing: * 1. It would run at O(logN) time instead of O(N) time. * 2. It always returns a value (it would return `data.length` if the * predicate returns `true` for all items). * * This function is the basis for all other sortedIndex functions which search * for a specific item in a sorted array, and it could be used to perform * similar efficient searches. * * `sortedIndex` - scans a sorted array with a binary search, find the first suitable index. * * `sortedIndexBy` - like `sortedIndex`, but assumes sorting is based on a callbackfn. * * `sortedLastIndex` - scans a sorted array with a binary search, finding the last suitable index. * * `sortedLastIndexBy` - like `sortedLastIndex`, but assumes sorting is based on a callbackfn. * * See also: * * `findIndex` - scans a possibly unsorted array in-order (linear search). * * `rankBy` - scans a possibly unsorted array in-order, returning the index based on a sorting criteria. * * @param data - Array, "sorted" by `predicate`. * @param predicate - A predicate which also defines the array's order. * @returns Index (In the range 0..data.length). * @signature * R.sortedIndexWith(data, predicate) * @example * R.sortedIndexWith(['a','ab','abc'], (item) => item.length < 2) // => 1 * @dataFirst * @category Array * @see findIndex, sortedIndex, sortedIndexBy, sortedLastIndex, sortedLastIndexBy */ declare function sortedIndexWith<T>(data: ReadonlyArray<T>, predicate: (value: T, index: number, data: ReadonlyArray<T>) => boolean): number; /** * Performs a **binary search** for the index of the item at which the predicate * stops returning `true`. This function assumes that the array is "sorted" in * regards to the predicate, meaning that running the predicate as a mapper on * it would result in an array `[...true[], ...false[]]`. * This stricter requirement from the predicate provides us 2 benefits over * `findIndex` which does a similar thing: * 1. It would run at O(logN) time instead of O(N) time. * 2. It always returns a value (it would return `data.length` if the * predicate returns `true` for all items). * * This function is the basis for all other sortedIndex functions which search * for a specific item in a sorted array, and it could be used to perform * similar efficient searches. * * `sortedIndex` - scans a sorted array with a binary search, find the first suitable index. * * `sortedIndexBy` - like `sortedIndex`, but assumes sorting is based on a callbackfn. * * `sortedLastIndex` - scans a sorted array with a binary search, finding the last suitable index. * * `sortedLastIndexBy` - like `sortedLastIndex`, but assumes sorting is based on a callbackfn. * * See also: * * `findIndex` - scans a possibly unsorted array in-order (linear search). * * `rankBy` - scans a possibly unsorted array in-order, returning the index based on a sorting criteria. * * @param predicate - A predicate which also defines the array's order. * @returns Index (In the range 0..data.length). * @signature * R.sortedIndexWith(predicate)(data) * @example * R.pipe(['a','ab','abc'], R.sortedIndexWith((item) => item.length < 2)) // => 1 * @dataLast * @category Array */ declare function sortedIndexWith<T>(predicate: (value: T, index: number, data: ReadonlyArray<T>) => boolean): (data: ReadonlyArray<T>) => number; //#endregion export { sortedIndexWith }; //# sourceMappingURL=sortedIndexWith-FUAgC-MZ.d.cts.map