-
Notifications
You must be signed in to change notification settings - Fork 363
/
status-utils.js
52 lines (48 loc) · 1.23 KB
/
status-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/** Status order: later elements have higher precedence. */
const statusOrder = [
'deprecated',
'draft',
'experimental',
'alpha',
'beta',
'stable'
]
/**
* Get the latest status, based on the order of precedence
*
* @param {readonly string[]} statuses list of status to pick from
* @returns {string | null} latest status if valid, `null` otherwise
*/
export const latestStatusFrom = (statuses) => {
return statuses
.filter(status => statusOrder.includes(status))
.sort(compareStatuses)
.at(-1) || null
}
/**
* Sort a list of statuses based on the order of precedence.
*
* @param {readonly string[]} statuses list of statuses
* @returns {string[]} the given statuses, sorted by precedence
*/
export const sortStatuses = (statuses) => {
return statuses.sort(compareStatuses)
}
/**
* Compare function, which is compatible with #Array.sort.
*
* @param {string} first
* @param {string} second
* @returns {1 | 0 | -1} sorting order
*/
export const compareStatuses = (first, second) => {
const firstRank = statusOrder.indexOf(first)
const secondRank = statusOrder.indexOf(second)
if (firstRank > secondRank) {
return 1
} else if (firstRank < secondRank) {
return -1
} else {
return 0
}
}