Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / internal / binaryIndexBy.js
1 /* Native method references for those with the same name as other `lodash` methods. */
2 var nativeFloor = Math.floor,
3     nativeMin = Math.min;
4
5 /** Used as references for the maximum length and index of an array. */
6 var MAX_ARRAY_LENGTH = 4294967295,
7     MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1;
8
9 /**
10  * This function is like `binaryIndex` except that it invokes `iteratee` for
11  * `value` and each element of `array` to compute their sort ranking. The
12  * iteratee is invoked with one argument; (value).
13  *
14  * @private
15  * @param {Array} array The sorted array to inspect.
16  * @param {*} value The value to evaluate.
17  * @param {Function} iteratee The function invoked per iteration.
18  * @param {boolean} [retHighest] Specify returning the highest qualified index.
19  * @returns {number} Returns the index at which `value` should be inserted
20  *  into `array`.
21  */
22 function binaryIndexBy(array, value, iteratee, retHighest) {
23   value = iteratee(value);
24
25   var low = 0,
26       high = array ? array.length : 0,
27       valIsNaN = value !== value,
28       valIsNull = value === null,
29       valIsUndef = value === undefined;
30
31   while (low < high) {
32     var mid = nativeFloor((low + high) / 2),
33         computed = iteratee(array[mid]),
34         isDef = computed !== undefined,
35         isReflexive = computed === computed;
36
37     if (valIsNaN) {
38       var setLow = isReflexive || retHighest;
39     } else if (valIsNull) {
40       setLow = isReflexive && isDef && (retHighest || computed != null);
41     } else if (valIsUndef) {
42       setLow = isReflexive && (retHighest || isDef);
43     } else if (computed == null) {
44       setLow = false;
45     } else {
46       setLow = retHighest ? (computed <= value) : (computed < value);
47     }
48     if (setLow) {
49       low = mid + 1;
50     } else {
51       high = mid;
52     }
53   }
54   return nativeMin(high, MAX_ARRAY_INDEX);
55 }
56
57 module.exports = binaryIndexBy;