]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/core-utils/miscs/miscs.ts
71703faacbd39acb32b5031b0502d5ae279c12b0
[github/Chocobozzz/PeerTube.git] / shared / core-utils / miscs / miscs.ts
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 export {
32 randomInt,
33 compareSemVer,
34 isPromise,
35 isCatchable
36 }