6e295f92fe7bbe9386161d7e7a1e5b982d982793
[yaffs-website] / node_modules / uncss / node_modules / lodash / internal / baseSortedIndexBy.js
1 /** Used as references for the maximum length and index of an array. */
2 var MAX_ARRAY_LENGTH = 4294967295,
3     MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1;
4
5 /* Built-in method references for those with the same name as other `lodash` methods. */
6 var nativeFloor = Math.floor,
7     nativeMin = Math.min;
8
9 /**
10  * The base implementation of `_.sortedIndexBy` and `_.sortedLastIndexBy`
11  * which invokes `iteratee` for `value` and each element of `array` to compute
12  * their sort ranking. The 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 iteratee invoked per element.
18  * @param {boolean} [retHighest] Specify returning the highest qualified index.
19  * @returns {number} Returns the index at which `value` should be inserted into `array`.
20  */
21 function baseSortedIndexBy(array, value, iteratee, retHighest) {
22   value = iteratee(value);
23
24   var low = 0,
25       high = array ? array.length : 0,
26       valIsNaN = value !== value,
27       valIsNull = value === null,
28       valIsUndef = value === undefined;
29
30   while (low < high) {
31     var mid = nativeFloor((low + high) / 2),
32         computed = iteratee(array[mid]),
33         isDef = computed !== undefined,
34         isReflexive = computed === computed;
35
36     if (valIsNaN) {
37       var setLow = isReflexive || retHighest;
38     } else if (valIsNull) {
39       setLow = isReflexive && isDef && (retHighest || computed != null);
40     } else if (valIsUndef) {
41       setLow = isReflexive && (retHighest || isDef);
42     } else if (computed == null) {
43       setLow = false;
44     } else {
45       setLow = retHighest ? (computed <= value) : (computed < value);
46     }
47     if (setLow) {
48       low = mid + 1;
49     } else {
50       high = mid;
51     }
52   }
53   return nativeMin(high, MAX_ARRAY_INDEX);
54 }
55
56 module.exports = baseSortedIndexBy;