Created
March 1, 2018 11:50
-
-
Save james-jlo-long/0ad9d074b15e37f5cce08631290bc0f9 to your computer and use it in GitHub Desktop.
Extremely basic Semantic Version comparison function.
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 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