Skip to content

Commit e441282

Browse files
committed
Convert a couple of files to TypeScript
1 parent 5344686 commit e441282

File tree

2 files changed

+25
-37
lines changed

2 files changed

+25
-37
lines changed

src/components/cylc/common/sort.js renamed to src/components/cylc/common/sort.ts

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,25 @@
1515
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18+
type Obj = string | Record<string, string>
19+
1820
/**
1921
* Declare function used in sortedIndexBy as a comparator.
2022
*
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
2724
*/
25+
type SortedIndexByComparator = (leftObject: Obj, leftValue: string, rightObject: Obj, rightValue: string) => number
26+
27+
type SortedIndexOptions = {
28+
comparator?: SortedIndexByComparator
29+
reverse?: boolean
30+
}
2831

2932
/**
3033
* The default comparator used to compare strings for cycle points, family proxies names,
3134
* task proxies names, and jobs.
32-
*
33-
* @param {string} left
34-
* @param {string} right
35-
* @returns {number}
36-
* @constructor
3735
*/
38-
export const DEFAULT_COMPARATOR = (left, right) => {
36+
export const DEFAULT_COMPARATOR = (left: string, right: string): number => {
3937
return left.toLowerCase().localeCompare(
4038
right.toLowerCase(),
4139
undefined,
@@ -48,11 +46,8 @@ export const DEFAULT_COMPARATOR = (left, right) => {
4846

4947
/**
5048
* Declare function used in sortedIndexBy for creating the iteratee.
51-
*
52-
* @callback SortedIndexByIteratee
53-
* @param {Object} value - any object
54-
* @returns {string}
5549
*/
50+
type SortedIndexByIteratee = (value: string | Record<string, string>) => string
5651

5752
/**
5853
* 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) => {
6762
* name, but that respects natural order for numbers, i.e. [1, 2, 10].
6863
* Not [1, 10, 2].
6964
*
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
7570
*/
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 {
7872
if (array.length === 0) {
7973
return 0
8074
}
81-
// If given a function, use it. Otherwise, simply use identity function.
82-
const iterateeFunction = iteratee || ((value) => value)
8375
// If given a function, use it. Otherwise, simply use locale sort with numeric enabled
8476
const comparatorFunction = options.comparator || ((leftObject, leftValue, rightObject, rightValue) => DEFAULT_COMPARATOR(leftValue, rightValue))
8577
let low = 0
8678
let high = array.length
8779

88-
const newValue = iterateeFunction(value)
80+
const newValue = iteratee(value)
8981

9082
while (low < high) {
9183
const mid = Math.floor((low + high) / 2)
92-
const midValue = iterateeFunction(array[mid])
84+
const midValue = iteratee(array[mid])
9385
let higher = comparatorFunction(value, newValue, array[mid], midValue)
9486
if (options.reverse) {
9587
higher = higher * -1

src/components/cylc/table/sort.js renamed to src/components/cylc/table/sort.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,17 @@
1818
/**
1919
* Comparator function for sorting datetime strings.
2020
*
21-
* @param {string} a - The first element for comparison.
22-
* @param {string} b - The second element for comparison.
23-
* @return {number} A number > 0 if a > b, or < 0 if a < b, or 0 if a === b
21+
* @param a - The first element for comparison.
22+
* @param b - The second element for comparison.
23+
* @return A number > 0 if a > b, or < 0 if a < b, or 0 if a === b
2424
*/
25-
export function datetimeComparator (a, b) {
26-
return new Date(a) - new Date(b)
25+
export function datetimeComparator (a: string | number, b: string | number): number {
26+
return new Date(a).valueOf() - new Date(b).valueOf()
2727
}
2828

2929
/**
3030
* Comparator function for sorting numbers.
31-
*
32-
* @param {number} a
33-
* @param {number} b
34-
* @returns {number}
3531
*/
36-
export function numberComparator (a, b) {
32+
export function numberComparator (a: number, b: number): number {
3733
return a - b
3834
}

0 commit comments

Comments
 (0)