Move to eslint
[oweals/peertube.git] / shared / core-utils / miscs / miscs.ts
1 function randomInt (low: number, high: number) {
2   return Math.floor(Math.random() * (high - low) + low)
3 }
4
5 // Thanks https://stackoverflow.com/a/16187766
6 function compareSemVer (a: string, b: string) {
7   const regExStrip0 = /(\.0+)+$/
8   const segmentsA = a.replace(regExStrip0, '').split('.')
9   const segmentsB = b.replace(regExStrip0, '').split('.')
10
11   const l = Math.min(segmentsA.length, segmentsB.length)
12
13   for (let i = 0; i < l; i++) {
14     const diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10)
15
16     if (diff) return diff
17   }
18
19   return segmentsA.length - segmentsB.length
20 }
21
22 function isPromise (value: any) {
23   return value && typeof value.then === 'function'
24 }
25
26 function isCatchable (value: any) {
27   return value && typeof value.catch === 'function'
28 }
29
30 export {
31   randomInt,
32   compareSemVer,
33   isPromise,
34   isCatchable
35 }