1 | var isSymbol = require('./isSymbol');
|
2 |
|
3 |
|
4 | var MAX_ARRAY_LENGTH = 4294967295,
|
5 | MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1;
|
6 |
|
7 |
|
8 | var nativeFloor = Math.floor,
|
9 | nativeMin = Math.min;
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function baseSortedIndexBy(array, value, iteratee, retHighest) {
|
25 | var low = 0,
|
26 | high = array == null ? 0 : array.length;
|
27 | if (high === 0) {
|
28 | return 0;
|
29 | }
|
30 |
|
31 | value = iteratee(value);
|
32 | var valIsNaN = value !== value,
|
33 | valIsNull = value === null,
|
34 | valIsSymbol = isSymbol(value),
|
35 | valIsUndefined = value === undefined;
|
36 |
|
37 | while (low < high) {
|
38 | var mid = nativeFloor((low + high) / 2),
|
39 | computed = iteratee(array[mid]),
|
40 | othIsDefined = computed !== undefined,
|
41 | othIsNull = computed === null,
|
42 | othIsReflexive = computed === computed,
|
43 | othIsSymbol = isSymbol(computed);
|
44 |
|
45 | if (valIsNaN) {
|
46 | var setLow = retHighest || othIsReflexive;
|
47 | } else if (valIsUndefined) {
|
48 | setLow = othIsReflexive && (retHighest || othIsDefined);
|
49 | } else if (valIsNull) {
|
50 | setLow = othIsReflexive && othIsDefined && (retHighest || !othIsNull);
|
51 | } else if (valIsSymbol) {
|
52 | setLow = othIsReflexive && othIsDefined && !othIsNull && (retHighest || !othIsSymbol);
|
53 | } else if (othIsNull || othIsSymbol) {
|
54 | setLow = false;
|
55 | } else {
|
56 | setLow = retHighest ? (computed <= value) : (computed < value);
|
57 | }
|
58 | if (setLow) {
|
59 | low = mid + 1;
|
60 | } else {
|
61 | high = mid;
|
62 | }
|
63 | }
|
64 | return nativeMin(high, MAX_ARRAY_INDEX);
|
65 | }
|
66 |
|
67 | module.exports = baseSortedIndexBy;
|