PeerTube/shared/core-utils/miscs/miscs.ts

36 lines
847 B
TypeScript
Raw Normal View History

2019-04-24 08:10:37 -05:00
function randomInt (low: number, high: number) {
return Math.floor(Math.random() * (high - low) + low)
}
// 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++) {
const diff = parseInt(segmentsA[ i ], 10) - parseInt(segmentsB[ i ], 10)
if (diff) return diff
}
return segmentsA.length - segmentsB.length
}
2019-07-18 07:28:37 -05:00
function isPromise (value: any) {
return value && typeof value.then === 'function'
}
function isCatchable (value: any) {
return value && typeof value.catch === 'function'
}
2019-04-24 08:10:37 -05:00
export {
randomInt,
2019-07-18 07:28:37 -05:00
compareSemVer,
isPromise,
isCatchable
2019-04-24 08:10:37 -05:00
}