]>
Commit | Line | Data |
---|---|---|
1 | // high excluded | |
2 | function randomInt (low: number, high: number) { | |
3 | return Math.floor(Math.random() * (high - low) + low) | |
4 | } | |
5 | ||
6 | // Thanks https://stackoverflow.com/a/16187766 | |
7 | function compareSemVer (a: string, b: string) { | |
8 | const regExStrip0 = /(\.0+)+$/ | |
9 | const segmentsA = a.replace(regExStrip0, '').split('.') | |
10 | const segmentsB = b.replace(regExStrip0, '').split('.') | |
11 | ||
12 | const l = Math.min(segmentsA.length, segmentsB.length) | |
13 | ||
14 | for (let i = 0; i < l; i++) { | |
15 | const diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10) | |
16 | ||
17 | if (diff) return diff | |
18 | } | |
19 | ||
20 | return segmentsA.length - segmentsB.length | |
21 | } | |
22 | ||
23 | function isPromise (value: any) { | |
24 | return value && typeof value.then === 'function' | |
25 | } | |
26 | ||
27 | function isCatchable (value: any) { | |
28 | return value && typeof value.catch === 'function' | |
29 | } | |
30 | ||
31 | function sortObjectComparator (key: string, order: 'asc' | 'desc') { | |
32 | return (a: any, b: any) => { | |
33 | if (a[key] < b[key]) { | |
34 | return order === 'asc' ? -1 : 1 | |
35 | } | |
36 | ||
37 | if (a[key] > b[key]) { | |
38 | return order === 'asc' ? 1 : -1 | |
39 | } | |
40 | ||
41 | return 0 | |
42 | } | |
43 | } | |
44 | ||
45 | export { | |
46 | randomInt, | |
47 | compareSemVer, | |
48 | isPromise, | |
49 | isCatchable, | |
50 | sortObjectComparator | |
51 | } |