Skip to content

Instantly share code, notes, and snippets.

@james-jlo-long
Created March 1, 2018 11:50
Show Gist options
  • Save james-jlo-long/0ad9d074b15e37f5cce08631290bc0f9 to your computer and use it in GitHub Desktop.
Save james-jlo-long/0ad9d074b15e37f5cce08631290bc0f9 to your computer and use it in GitHub Desktop.
Extremely basic Semantic Version comparison function.
function SemVer(version) {
var parsed = String(version).match(/^(\d+)\.(\d+)\.(\d+)$/) || [];
this.string = version;
this.version = [
Number(parsed[1]) || 0,
Number(parsed[2]) || 0,
Number(parsed[3]) || 0
];
}
SemVer.prototype = {
compare: function (version) {
var semver = new SemVer(version);
return (
this.version[0] - semver.version[0]
|| this.version[1] - semver.version[1]
|| this.version[2] - semver.version[2]
);
},
toString: function () {
return this.string;
}
};
SemVer.compare = function (version1, version2) {
return (new SemVer(version1)).compare(version2);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment