compare-version.js 436 B

123456789101112131415161718192021
  1. export default function compareVersion(v1, v2) {
  2. v1 = v1.split('.')
  3. v2 = v2.split('.')
  4. const len = Math.max(v1.length, v2.length)
  5. while (v1.length < len) {
  6. v1.push('0')
  7. }
  8. while (v2.length < len) {
  9. v2.push('0')
  10. }
  11. for (let i = 0; i < len; i++) {
  12. const num1 = parseInt(v1[i])
  13. const num2 = parseInt(v2[i])
  14. if (num1 > num2) {
  15. return 1
  16. } if (num1 < num2) {
  17. return -1
  18. }
  19. }
  20. return 0
  21. }