compare-version.js 455 B

12345678910111213141516171819202122232425262728
  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. }
  17. if (num1 < num2) {
  18. return -1;
  19. }
  20. }
  21. return 0;
  22. }