2020-12-10 16:38:12 +01:00
|
|
|
// high excluded
|
2019-04-24 15:10:37 +02:00
|
|
|
function randomInt (low: number, high: number) {
|
|
|
|
return Math.floor(Math.random() * (high - low) + low)
|
|
|
|
}
|
|
|
|
|
2019-07-16 11:33:22 +02:00
|
|
|
// Thanks https://stackoverflow.com/a/16187766
|
|
|
|
function compareSemVer (a: string, b: string) {
|
|
|
|
const regExStrip0 = /(\.0+)+$/
|
|
|
|
const segmentsA = a.replace(regExStrip0, '').split('.')
|
|
|
|
const segmentsB = b.replace(regExStrip0, '').split('.')
|
|
|
|
|
|
|
|
const l = Math.min(segmentsA.length, segmentsB.length)
|
|
|
|
|
|
|
|
for (let i = 0; i < l; i++) {
|
2020-01-31 16:56:52 +01:00
|
|
|
const diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10)
|
2019-07-16 11:33:22 +02:00
|
|
|
|
|
|
|
if (diff) return diff
|
|
|
|
}
|
|
|
|
|
|
|
|
return segmentsA.length - segmentsB.length
|
|
|
|
}
|
|
|
|
|
2019-07-18 14:28:37 +02:00
|
|
|
function isPromise (value: any) {
|
|
|
|
return value && typeof value.then === 'function'
|
|
|
|
}
|
|
|
|
|
|
|
|
function isCatchable (value: any) {
|
|
|
|
return value && typeof value.catch === 'function'
|
|
|
|
}
|
|
|
|
|
2021-05-27 15:59:55 +02:00
|
|
|
function sortObjectComparator (key: string, order: 'asc' | 'desc') {
|
|
|
|
return (a: any, b: any) => {
|
|
|
|
if (a[key] < b[key]) {
|
|
|
|
return order === 'asc' ? -1 : 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a[key] > b[key]) {
|
|
|
|
return order === 'asc' ? 1 : -1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 15:10:37 +02:00
|
|
|
export {
|
2019-07-16 11:33:22 +02:00
|
|
|
randomInt,
|
2019-07-18 14:28:37 +02:00
|
|
|
compareSemVer,
|
|
|
|
isPromise,
|
2021-05-27 15:59:55 +02:00
|
|
|
isCatchable,
|
|
|
|
sortObjectComparator
|
2019-04-24 15:10:37 +02:00
|
|
|
}
|