2
1

miscs.ts 847 B

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