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 ( )
40
38
. localeCompare (
41
39
right . toLowerCase ( ) ,
@@ -49,11 +47,8 @@ export const DEFAULT_COMPARATOR = (left, right) => {
49
47
50
48
/**
51
49
* Declare function used in sortedIndexBy for creating the iteratee.
52
- *
53
- * @callback SortedIndexByIteratee
54
- * @param {Object } value - any object
55
- * @returns {string }
56
50
*/
51
+ type SortedIndexByIteratee = ( value : string | Record < string , string > ) => string
57
52
58
53
/**
59
54
* Given a list of elements, and a value to be added to the list, we
@@ -68,29 +63,26 @@ export const DEFAULT_COMPARATOR = (left, right) => {
68
63
* name, but that respects natural order for numbers, i.e. [1, 2, 10].
69
64
* Not [1, 10, 2].
70
65
*
71
- * @param { Object[] } array - list of string values, or of objects with string values
72
- * @param { Object } value - a value to be inserted in the list, or an object wrapping the value (see iteratee)
73
- * @param { SortedIndexByIteratee= } iteratee - an optional function used to return the value of the element of the list}
74
- * @param { SortedIndexByComparator= } comparator - function used to compare the newValue with otherValues in the list
75
- * @return { number } - sorted index
66
+ * @param array - list of string values, or of objects with string values
67
+ * @param value - a value to be inserted in the list, or an object wrapping the value (see iteratee)
68
+ * @param iteratee - an optional function used to return the value of the element of the list
69
+ * @param comparator - function used to compare the newValue with otherValues in the list
70
+ * @return sorted index
76
71
*/
77
- export function sortedIndexBy ( array , value , iteratee , options = { } ) {
78
- // comparator, reverse = false) {
72
+ export function sortedIndexBy ( array : Obj [ ] , value : Obj , iteratee : SortedIndexByIteratee , options : SortedIndexOptions = { } ) : number {
79
73
if ( array . length === 0 ) {
80
74
return 0
81
75
}
82
- // If given a function, use it. Otherwise, simply use identity function.
83
- const iterateeFunction = iteratee || ( ( value ) => value )
84
76
// If given a function, use it. Otherwise, simply use locale sort with numeric enabled
85
77
const comparatorFunction = options . comparator || ( ( leftObject , leftValue , rightObject , rightValue ) => DEFAULT_COMPARATOR ( leftValue , rightValue ) )
86
78
let low = 0
87
79
let high = array . length
88
80
89
- const newValue = iterateeFunction ( value )
81
+ const newValue = iteratee ( value )
90
82
91
83
while ( low < high ) {
92
84
const mid = Math . floor ( ( low + high ) / 2 )
93
- const midValue = iterateeFunction ( array [ mid ] )
85
+ const midValue = iteratee ( array [ mid ] )
94
86
let higher = comparatorFunction ( value , newValue , array [ mid ] , midValue )
95
87
if ( options . reverse ) {
96
88
higher = higher * - 1
0 commit comments