[d565449] | 1 | import isSymbol from './isSymbol.js';
|
---|
| 2 |
|
---|
| 3 | /** Used as references for the maximum length and index of an array. */
|
---|
| 4 | var MAX_ARRAY_LENGTH = 4294967295,
|
---|
| 5 | MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1;
|
---|
| 6 |
|
---|
| 7 | /* Built-in method references for those with the same name as other `lodash` methods. */
|
---|
| 8 | var nativeFloor = Math.floor,
|
---|
| 9 | nativeMin = Math.min;
|
---|
| 10 |
|
---|
| 11 | /**
|
---|
| 12 | * The base implementation of `_.sortedIndexBy` and `_.sortedLastIndexBy`
|
---|
| 13 | * which invokes `iteratee` for `value` and each element of `array` to compute
|
---|
| 14 | * their sort ranking. The iteratee is invoked with one argument; (value).
|
---|
| 15 | *
|
---|
| 16 | * @private
|
---|
| 17 | * @param {Array} array The sorted array to inspect.
|
---|
| 18 | * @param {*} value The value to evaluate.
|
---|
| 19 | * @param {Function} iteratee The iteratee invoked per element.
|
---|
| 20 | * @param {boolean} [retHighest] Specify returning the highest qualified index.
|
---|
| 21 | * @returns {number} Returns the index at which `value` should be inserted
|
---|
| 22 | * into `array`.
|
---|
| 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 | export default baseSortedIndexBy;
|
---|