1 var binaryIndexBy = require('./binaryIndexBy'),
2 identity = require('../utility/identity');
4 /** Used as references for the maximum length and index of an array. */
5 var MAX_ARRAY_LENGTH = 4294967295,
6 HALF_MAX_ARRAY_LENGTH = MAX_ARRAY_LENGTH >>> 1;
9 * Performs a binary search of `array` to determine the index at which `value`
10 * should be inserted into `array` in order to maintain its sort order.
13 * @param {Array} array The sorted array to inspect.
14 * @param {*} value The value to evaluate.
15 * @param {boolean} [retHighest] Specify returning the highest qualified index.
16 * @returns {number} Returns the index at which `value` should be inserted
19 function binaryIndex(array, value, retHighest) {
21 high = array ? array.length : low;
23 if (typeof value == 'number' && value === value && high <= HALF_MAX_ARRAY_LENGTH) {
25 var mid = (low + high) >>> 1,
26 computed = array[mid];
28 if ((retHighest ? (computed <= value) : (computed < value)) && computed !== null) {
36 return binaryIndexBy(array, value, identity, retHighest);
39 module.exports = binaryIndex;