Skip to content

Instantly share code, notes, and snippets.

@felipap
Created December 20, 2015 22:22
Show Gist options
  • Save felipap/450df185c9e54eac1a2a to your computer and use it in GitHub Desktop.
Save felipap/450df185c9e54eac1a2a to your computer and use it in GitHub Desktop.
Compares two semvers.
function cmpSemver(v1, v2) {
if (v1 === "") {
return v2 === "" ? 0 : -1;
} else if (v2 === "") {
return 1;
}
var e1 = v1.match(/^(\d+)\.?(.*)/), e2 = v2.match(/^(\d+)\.?(.*)/);
if (parseInt(e1[1]) === parseInt(e2[1]))
return cmpVersions(e1[2], e2[2]);
return parseInt(e1[1]) > parseInt(e2[1]) ? 1 : -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment