| 1 | import { isNull } from '../../predicate/isNull.mjs';
|
|---|
| 2 | import { isUndefined } from '../../predicate/isUndefined.mjs';
|
|---|
| 3 | import { identity } from '../function/identity.mjs';
|
|---|
| 4 | import { isNaN } from '../predicate/isNaN.mjs';
|
|---|
| 5 | import { isNil } from '../predicate/isNil.mjs';
|
|---|
| 6 | import { isSymbol } from '../predicate/isSymbol.mjs';
|
|---|
| 7 | import { iteratee } from '../util/iteratee.mjs';
|
|---|
| 8 |
|
|---|
| 9 | const MAX_ARRAY_LENGTH = 4294967295;
|
|---|
| 10 | const MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1;
|
|---|
| 11 | function sortedIndexBy(array, value, iteratee$1 = identity, retHighest) {
|
|---|
| 12 | if (isNil(array) || array.length === 0) {
|
|---|
| 13 | return 0;
|
|---|
| 14 | }
|
|---|
| 15 | let low = 0;
|
|---|
| 16 | let high = array.length;
|
|---|
| 17 | const iterateeFunction = iteratee(iteratee$1);
|
|---|
| 18 | const transformedValue = iterateeFunction(value);
|
|---|
| 19 | const valIsNaN = isNaN(transformedValue);
|
|---|
| 20 | const valIsNull = isNull(transformedValue);
|
|---|
| 21 | const valIsSymbol = isSymbol(transformedValue);
|
|---|
| 22 | const valIsUndefined = isUndefined(transformedValue);
|
|---|
| 23 | while (low < high) {
|
|---|
| 24 | let setLow;
|
|---|
| 25 | const mid = Math.floor((low + high) / 2);
|
|---|
| 26 | const computed = iterateeFunction(array[mid]);
|
|---|
| 27 | const othIsDefined = !isUndefined(computed);
|
|---|
| 28 | const othIsNull = isNull(computed);
|
|---|
| 29 | const othIsReflexive = !isNaN(computed);
|
|---|
| 30 | const othIsSymbol = isSymbol(computed);
|
|---|
| 31 | if (valIsNaN) {
|
|---|
| 32 | setLow = retHighest || othIsReflexive;
|
|---|
| 33 | }
|
|---|
| 34 | else if (valIsUndefined) {
|
|---|
| 35 | setLow = othIsReflexive && (retHighest || othIsDefined);
|
|---|
| 36 | }
|
|---|
| 37 | else if (valIsNull) {
|
|---|
| 38 | setLow = othIsReflexive && othIsDefined && (retHighest || !othIsNull);
|
|---|
| 39 | }
|
|---|
| 40 | else if (valIsSymbol) {
|
|---|
| 41 | setLow = othIsReflexive && othIsDefined && !othIsNull && (retHighest || !othIsSymbol);
|
|---|
| 42 | }
|
|---|
| 43 | else if (othIsNull || othIsSymbol) {
|
|---|
| 44 | setLow = false;
|
|---|
| 45 | }
|
|---|
| 46 | else {
|
|---|
| 47 | setLow = retHighest ? computed <= transformedValue : computed < transformedValue;
|
|---|
| 48 | }
|
|---|
| 49 | if (setLow) {
|
|---|
| 50 | low = mid + 1;
|
|---|
| 51 | }
|
|---|
| 52 | else {
|
|---|
| 53 | high = mid;
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 | return Math.min(high, MAX_ARRAY_INDEX);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | export { sortedIndexBy };
|
|---|