15
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
*/
17
17
18
+ type Obj = string | Record < string , string >
19
+
18
20
/**
19
21
* Declare function used in sortedIndexBy as a comparator.
20
22
*
21
- * @callback SortedIndexByComparator
22
- * @param {Object } leftObject - left parameter object
23
- * @param {string } leftValue - left parameter value
24
- * @param {Object } rightObject - right parameter object
25
- * @param {string } rightValue - right parameter value
26
- * @returns {boolean } - true if leftValue is higher than rightValue
23
+ * @returns positive int if leftValue is higher than rightValue
27
24
*/
25
+ type SortedIndexByComparator = ( leftObject : Obj , leftValue : string , rightObject : Obj , rightValue : string ) => number
26
+
27
+ type SortedIndexOptions = {
28
+ comparator ?: SortedIndexByComparator
29
+ reverse ?: boolean
30
+ }
28
31
29
32
/**
30
33
* The default comparator used to compare strings for cycle points, family proxies names,
31
34
* task proxies names, and jobs.
32
- *
33
- * @param {string } left
34
- * @param {string } right
35
- * @returns {number }
36
- * @constructor
37
35
*/
38
- export const DEFAULT_COMPARATOR = ( left , right ) => {
36
+ export const DEFAULT_COMPARATOR = ( left : string , right : string ) : number => {
39
37
return left . toLowerCase ( ) . localeCompare (
40
38
right . toLowerCase ( ) ,
41
39
undefined ,
@@ -48,11 +46,8 @@ export const DEFAULT_COMPARATOR = (left, right) => {
48
46
49
47
/**
50
48
* Declare function used in sortedIndexBy for creating the iteratee.
51
- *
52
- * @callback SortedIndexByIteratee
53
- * @param {Object } value - any object
54
- * @returns {string }
55
49
*/
50
+ type SortedIndexByIteratee = ( value : string | Record < string , string > ) => string
56
51
57
52
/**
58
53
* Given a list of elements, and a value to be added to the list, we
@@ -67,29 +62,26 @@ export const DEFAULT_COMPARATOR = (left, right) => {
67
62
* name, but that respects natural order for numbers, i.e. [1, 2, 10].
68
63
* Not [1, 10, 2].
69
64
*
70
- * @param { Object[] } array - list of string values, or of objects with string values
71
- * @param { Object } value - a value to be inserted in the list, or an object wrapping the value (see iteratee)
72
- * @param { SortedIndexByIteratee= } iteratee - an optional function used to return the value of the element of the list}
73
- * @param { SortedIndexByComparator= } comparator - function used to compare the newValue with otherValues in the list
74
- * @return { number } - sorted index
65
+ * @param array - list of string values, or of objects with string values
66
+ * @param value - a value to be inserted in the list, or an object wrapping the value (see iteratee)
67
+ * @param iteratee - an optional function used to return the value of the element of the list
68
+ * @param comparator - function used to compare the newValue with otherValues in the list
69
+ * @return sorted index
75
70
*/
76
- export function sortedIndexBy ( array , value , iteratee , options = { } ) {
77
- // comparator, reverse = false) {
71
+ export function sortedIndexBy ( array : Obj [ ] , value : Obj , iteratee : SortedIndexByIteratee , options : SortedIndexOptions = { } ) : number {
78
72
if ( array . length === 0 ) {
79
73
return 0
80
74
}
81
- // If given a function, use it. Otherwise, simply use identity function.
82
- const iterateeFunction = iteratee || ( ( value ) => value )
83
75
// If given a function, use it. Otherwise, simply use locale sort with numeric enabled
84
76
const comparatorFunction = options . comparator || ( ( leftObject , leftValue , rightObject , rightValue ) => DEFAULT_COMPARATOR ( leftValue , rightValue ) )
85
77
let low = 0
86
78
let high = array . length
87
79
88
- const newValue = iterateeFunction ( value )
80
+ const newValue = iteratee ( value )
89
81
90
82
while ( low < high ) {
91
83
const mid = Math . floor ( ( low + high ) / 2 )
92
- const midValue = iterateeFunction ( array [ mid ] )
84
+ const midValue = iteratee ( array [ mid ] )
93
85
let higher = comparatorFunction ( value , newValue , array [ mid ] , midValue )
94
86
if ( options . reverse ) {
95
87
higher = higher * - 1
0 commit comments