miscs.ts 860 B

123456789101112131415161718192021222324252627282930313233343536
  1. // high excluded
  2. function randomInt (low: number, high: number) {
  3. return Math.floor(Math.random() * (high - low) + low)
  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. const l = Math.min(segmentsA.length, segmentsB.length)
  11. for (let i = 0; i < l; i++) {
  12. const diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10)
  13. if (diff) return diff
  14. }
  15. return segmentsA.length - segmentsB.length
  16. }
  17. function isPromise (value: any) {
  18. return value && typeof value.then === 'function'
  19. }
  20. function isCatchable (value: any) {
  21. return value && typeof value.catch === 'function'
  22. }
  23. export {
  24. randomInt,
  25. compareSemVer,
  26. isPromise,
  27. isCatchable
  28. }