Created
December 21, 2020 15:35
-
-
Save bitsmanent/4879606a5184e811c0d6a4ff3b202338 to your computer and use it in GitHub Desktop.
Compare software release version
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
/* E.g. verscmp("1.2.3", "1.2.2") */ | |
function verscmp(a, b) { | |
var spa = a.split('.').map(x => Number(x)); | |
var spb = b.split('.').map(x => Number(x)); | |
var part = [], r = 0; | |
while(!r && spa.length && spb.length) { | |
part = [spa.shift(), spb.shift()]; | |
if(part[0] == part[1]) | |
continue; | |
r = part[0] > part[1] ? -1 : 1; | |
break; | |
} | |
if(!r && (spa.length || spb.length)) { | |
/* ignore any leading zeros, e.g. 0.1 == 0.1.0 */ | |
spa = spa.filter(x => x); | |
spb = spb.filter(x => x); | |
if(spa.length || spb.length) | |
return spa.length ? -1 : 1; | |
} | |
return r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment