Skip to content

Commit a214758

Browse files
committed
Convert a couple of files to TypeScript
1 parent 293f6fa commit a214758

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
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()
4038
.localeCompare(
4139
right.toLowerCase(),
@@ -49,11 +47,8 @@ export const DEFAULT_COMPARATOR = (left, right) => {
4947

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

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

89-
const newValue = iterateeFunction(value)
81+
const newValue = iteratee(value)
9082

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

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
* strings are treated as infinity.
2121
*
2222
* @export
23-
* @param {*} a - The first element for comparison.
24-
* @param {*} b - The second element for comparison.
25-
* @return {number} A number > 0 if a > b, or < 0 if a < b, or 0 if a === b
23+
* @param a - The first element for comparison.
24+
* @param b - The second element for comparison.
25+
* @return A number > 0 if a > b, or < 0 if a < b, or 0 if a === b
2626
*/
27-
export function datetimeComparator (a, b) {
27+
export function datetimeComparator (a: string | number, b: string | number): number {
2828
a = (a ?? '') === '' ? Infinity : new Date(a).getTime()
2929
b = (b ?? '') === '' ? Infinity : new Date(b).getTime()
3030
// Avoid return NaN for a === b === Infinity

0 commit comments

Comments
 (0)