diff --git a/src/utils/binary-search.js b/src/utils/binary-search.js deleted file mode 100644 index 87bd46828b4..00000000000 --- a/src/utils/binary-search.js +++ /dev/null @@ -1,41 +0,0 @@ -let BinarySearch = { - /** - * Searches for an item in an array which matches a certain condition. - * This requires the condition to only match one item in the array, - * and for the array to be ordered. - * - * @param {Array} list The array to search. - * @param {Function} comparisonFunction - * Called and provided a candidate item as the first argument. - * Should return: - * > -1 if the item should be located at a lower index than the provided item. - * > 1 if the item should be located at a higher index than the provided item. - * > 0 if the item is the item you're looking for. - * - * @return {*} The object if it is found or null otherwise. - */ - search: function (list, comparisonFunction) { - let minIndex = 0; - let maxIndex = list.length - 1; - let currentIndex = null; - let currentElement = null; - - while (minIndex <= maxIndex) { - currentIndex = (minIndex + maxIndex) / 2 | 0; - currentElement = list[currentIndex]; - - let comparisonResult = comparisonFunction(currentElement); - if (comparisonResult > 0) { - minIndex = currentIndex + 1; - } else if (comparisonResult < 0) { - maxIndex = currentIndex - 1; - } else { - return currentElement; - } - } - - return null; - } -}; - -export default BinarySearch; diff --git a/src/utils/binary-search.ts b/src/utils/binary-search.ts new file mode 100644 index 00000000000..01c8b492ada --- /dev/null +++ b/src/utils/binary-search.ts @@ -0,0 +1,44 @@ +type BinarySearchComparison < T > = (candidate: T) => -1 | 0 | 1; + +const BinarySearch = { + + /** + * Searches for an item in an array which matches a certain condition. + * This requires the condition to only match one item in the array, + * and for the array to be ordered. + * + * @param {Array} list The array to search. + * @param {BinarySearchComparison} comparisonFn + * Called and provided a candidate item as the first argument. + * Should return: + * > -1 if the item should be located at a lower index than the provided item. + * > 1 if the item should be located at a higher index than the provided item. + * > 0 if the item is the item you're looking for. + * + * @return {T | null} The object if it is found or null otherwise. + */ + search: function (list: T[], comparisonFn: BinarySearchComparison): T | null { + let minIndex: number = 0; + let maxIndex: number = list.length - 1; + let currentIndex: number | null = null; + let currentElement: T | null = null; + + while (minIndex <= maxIndex) { + currentIndex = (minIndex + maxIndex) / 2 | 0; + currentElement = list[currentIndex]; + + let comparisonResult = comparisonFn(currentElement); + if (comparisonResult > 0) { + minIndex = currentIndex + 1; + } else if (comparisonResult < 0) { + maxIndex = currentIndex - 1; + } else { + return currentElement; + } + } + + return null; + } +}; + +export default BinarySearch;