Versions.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* eslint-disable
  2. no-unused-vars,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. // Compare Versions like 1.2 < 4.1
  14. const convertToArray = v => Array.from(v.split('.')).map(x => parseInt(x, 10))
  15. const cmp = function (v1, v2) {
  16. // allow comparison to work with integers
  17. if (typeof v1 === 'number' && typeof v2 === 'number') {
  18. if (v1 > v2) {
  19. return +1
  20. }
  21. if (v1 < v2) {
  22. return -1
  23. }
  24. // otherwise equal
  25. return 0
  26. }
  27. // comparison with strings
  28. v1 = convertToArray(v1)
  29. v2 = convertToArray(v2)
  30. while (v1.length || v2.length) {
  31. const [x, y] = Array.from([v1.shift(), v2.shift()])
  32. if (x > y) {
  33. return +1
  34. }
  35. if (x < y) {
  36. return -1
  37. }
  38. if (x != null && y == null) {
  39. return +1
  40. }
  41. if (x == null && y != null) {
  42. return -1
  43. }
  44. }
  45. return 0
  46. }
  47. export function compare(v1, v2) {
  48. return cmp(v1, v2)
  49. }
  50. export function gt(v1, v2) {
  51. return cmp(v1, v2) > 0
  52. }
  53. export function lt(v1, v2) {
  54. return cmp(v1, v2) < 0
  55. }
  56. export function gte(v1, v2) {
  57. return cmp(v1, v2) >= 0
  58. }
  59. export function lte(v1, v2) {
  60. return cmp(v1, v2) <= 0
  61. }