Created
July 17, 2017 07:59
-
-
Save abdurrahmanekr/8ec79b04226af6da78fc4105152b9e07 to your computer and use it in GitHub Desktop.
Versiyon kontrol fonksiyonu
This file contains 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
function versionCompare(v1, v2, options) { | |
var lexicographical = options && options.lexicographical, | |
zeroExtend = options && options.zeroExtend, | |
v1parts = v1.split('.'), | |
v2parts = v2.split('.'); | |
function isValidPart(x) { | |
return (lexicographical ? /^\d+[A-Za-z]*$/ : /^\d+$/).test(x); | |
} | |
if (!v1parts.every(isValidPart) || !v2parts.every(isValidPart)) { | |
return NaN; | |
} | |
if (zeroExtend) { | |
while (v1parts.length < v2parts.length) v1parts.push("0"); | |
while (v2parts.length < v1parts.length) v2parts.push("0"); | |
} | |
if (!lexicographical) { | |
v1parts = v1parts.map(Number); | |
v2parts = v2parts.map(Number); | |
} | |
for (var i = 0; i < v1parts.length; ++i) { | |
if (v2parts.length == i) { | |
return 1; | |
} | |
if (v1parts[i] == v2parts[i]) { | |
continue; | |
} | |
else if (v1parts[i] > v2parts[i]) { | |
return 1; | |
} | |
else { | |
return -1; | |
} | |
} | |
if (v1parts.length != v2parts.length) { | |
return -1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment