source: node_modules/es-toolkit/dist/compat/array/sortedIndexBy.mjs

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 2.1 KB
Line 
1import { isNull } from '../../predicate/isNull.mjs';
2import { isUndefined } from '../../predicate/isUndefined.mjs';
3import { identity } from '../function/identity.mjs';
4import { isNaN } from '../predicate/isNaN.mjs';
5import { isNil } from '../predicate/isNil.mjs';
6import { isSymbol } from '../predicate/isSymbol.mjs';
7import { iteratee } from '../util/iteratee.mjs';
8
9const MAX_ARRAY_LENGTH = 4294967295;
10const MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1;
11function 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
59export { sortedIndexBy };
Note: See TracBrowser for help on using the repository browser.