compareVersion.js 639 B

1234567891011121314151617181920212223
  1. export function compareVersion(v1, v2) {
  2. if (v1 === v2)
  3. return 0;
  4. var v1Arr = v1.split('.');
  5. var v2Arr = v2.split('.');
  6. var len = v1Arr.length < v2Arr.length ? v1Arr.length : v2Arr.length;
  7. var i = 0;
  8. while (i <= len) {
  9. if (v1Arr[i] === v2Arr[i]) {
  10. i++;
  11. }
  12. else if (!v1Arr[i] || !v2Arr[i]) {
  13. return v1Arr.length > v2Arr.length ? 1 : -1;
  14. }
  15. else {
  16. return Number(v1Arr[i]) === Number(v2Arr[i])
  17. ? 0
  18. : Number(v1Arr[i]) > Number(v2Arr[i])
  19. ? 1
  20. : -1;
  21. }
  22. }
  23. }