Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
new compareVersion method
var notSmaller, arrayA = versionA.split('-'), arrayB = versionB.split('-'),
minorA, minorB, mainA = arrayA[0].split('.'), mainB = arrayB[0].split('.'),
len = (mainA.length <= mainB.length) ? mainA.length : mainB.length;
for (var i = 0; i < len; ++i) {
if (parseInt(mainA[i], 10) < parseInt(mainB[i], 10)) {
notSmaller = -1;
break;
} else if (parseInt(mainA[i], 10) > parseInt(mainB[i], 10)) {
notSmaller = 1;
break;
} else if (i === len - 1) {
if (mainA.length > mainB.length) {
notSmaller = 1;
} else if (mainA.length === mainB.length) {
if (arrayA.length === arrayB.length === 1) {
notSmaller = 0;
} else if (arrayA.length > arrayB.length) {
notSmaller = 1;
} else if (arrayA.length < arrayA.length) {
notSmaller = -1;
} else {
minorA = arrayA.reverse()[0].replace(/[a-zA-Z]/,'');
minorB = arrayB.reverse()[0].replace(/[a-zA-Z]/,'');
if (parseInt(minorA, 10) === parseInt(minorB, 10)) {
notSmaller = 0;
} else if (parseInt(minorA, 10) > parseInt(minorB, 10)) {
notSmaller = 1;
} else if (parseInt(minorA, 10) < parseInt(minorB, 10)) {
notSmaller = -1;
}
}
} else {
notSmaller = -1;
}
}
}
return notSmaller;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.