UNPKG

@newdash/newdash

Version:

javascript/typescript utility library

45 lines (44 loc) 1.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.baseSortedIndex = void 0; const baseSortedIndexBy_1 = __importDefault(require("./baseSortedIndexBy")); const isSymbol_1 = __importDefault(require("../isSymbol")); /** Used as references for the maximum length and index of an array. */ const MAX_ARRAY_LENGTH = 4294967295; const HALF_MAX_ARRAY_LENGTH = MAX_ARRAY_LENGTH >>> 1; /** * The base implementation of `sortedIndex` and `sortedLastIndex` which * performs a binary search of `array` to determine the index at which `value` * should be inserted into `array` in order to maintain its sort order. * * @private * @param {Array} array The sorted array to inspect. * @param {*} value The value to evaluate. * @param {boolean} [retHighest] Specify returning the highest qualified index. * @returns {number} Returns the index at which `value` should be inserted * into `array`. */ function baseSortedIndex(array, value, retHighest) { let low = 0; let high = array == null ? low : array.length; if (typeof value === 'number' && value === value && high <= HALF_MAX_ARRAY_LENGTH) { while (low < high) { const mid = (low + high) >>> 1; const computed = array[mid]; if (computed !== null && !(0, isSymbol_1.default)(computed) && (retHighest ? (computed <= value) : (computed < value))) { low = mid + 1; } else { high = mid; } } return high; } return (0, baseSortedIndexBy_1.default)(array, value, (value) => value, retHighest); } exports.baseSortedIndex = baseSortedIndex; exports.default = baseSortedIndex;