Created
February 11, 2015 16:31
-
-
Save zfkun/074e0488f0119a97a8f1 to your computer and use it in GitHub Desktop.
version compare toolkit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 版本对比 | |
* | |
* @param {string} a 对比版本号 | |
* @param {string} b 参考版本号 | |
* @return {number} `1`(`a > b`) || `-1`(`a < b`) || `0`(`a = b`) | |
*/ | |
function versionCompare(a, b) { | |
a = a.split('.'); | |
b = b.split('.'); | |
for (var i = 0, n = Math.max(a.length, b.length); i < n; i++) { | |
a[i] = parseInt(a[i], 10) || 0; | |
b[i] = parseInt(b[i], 10) || 0; | |
if (a[i] === b[i]) { | |
continue; | |
} | |
return a[i] > b[i] ? 1 : -1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment