vercomp.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. define([], function () {
  2. function order(c) {
  3. if (/^\d$/.test(c))
  4. return 0
  5. else if (/^[a-z]$/i.test(c))
  6. return c.charCodeAt(0)
  7. else if (c === "~")
  8. return -1
  9. else if (c)
  10. return c.charCodeAt(0) + 256
  11. else
  12. return 0
  13. }
  14. // Based on dpkg code
  15. function vercomp(a, b) {
  16. var apos = 0, bpos = 0
  17. while (apos < a.length || bpos < b.length) {
  18. var firstDiff = 0
  19. while ((apos < a.length && !/^\d$/.test(a[apos])) || (bpos < b.length && !/^\d$/.test(b[bpos]))) {
  20. var ac = order(a[apos])
  21. var bc = order(b[bpos])
  22. if (ac !== bc)
  23. return ac - bc
  24. apos++
  25. bpos++
  26. }
  27. while (a[apos] === "0")
  28. apos++
  29. while (b[bpos] === "0")
  30. bpos++
  31. while (/^\d$/.test(a[apos]) && /^\d$/.test(b[bpos])) {
  32. if (firstDiff === 0)
  33. firstDiff = a.charCodeAt(apos) - b.charCodeAt(bpos)
  34. apos++
  35. bpos++
  36. }
  37. if (/^\d$/.test(a[apos]))
  38. return 1
  39. if (/^\d$/.test(b[bpos]))
  40. return -1
  41. if (firstDiff !== 0)
  42. return firstDiff
  43. }
  44. return 0
  45. }
  46. return vercomp
  47. })