UNPKG

@tvkitchen/countertop

Version:

The entry point for developers who want to set up a TV Kitchen.

31 lines (30 loc) 922 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sortedIndexBy = void 0; /** * Find the index within a sorted array to which a given value could be inserted while * still maintaining order. * * This takes in a function that will be called on array items in order to parse the sorted * value. */ const sortedIndexBy = (sortedArray, value, valueGetter, returnHighest = false) => { let low = 0; let high = sortedArray.length; while (low < high) { const mid = Math.floor((low + high) / 2); const computed = valueGetter(sortedArray[mid]); if (computed < value) { low = mid + 1; } else if (computed === value && returnHighest) { low = mid + 1; } else { high = mid; } } return Math.min(high, sortedArray.length); }; exports.sortedIndexBy = sortedIndexBy;