Last active
August 29, 2015 13:56
-
-
Save mikaelkaron/9205892 to your computer and use it in GitHub Desktop.
Parsing a semver string and translating values
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
"1.2.3-pre+build".match(/(\d+)(?:\.(\d+)(?:\.(\d+))?)?(?:-([^+]+))?(?:\+(.+))?/).map(function (value, index) { return index > 0 && index < 4 ? parseInt(value || 0) : value; }); |
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
[ "3.0.1", "3.0.2-1+build", "1.1", "2.1" ] | |
.map(function (semver) { | |
return (/(\d+)(?:\.(\d+)(?:\.(\d+))?)?(?:-([^+]+))?(?:\+(.+))?/.exec(semver) || []) | |
.map(function (value, index) { | |
return index > 0 && index < 4 | |
? parseInt(value || 0) | |
: value; | |
}); | |
}) | |
.sort(function (b, a) { | |
var major = a[1] - b[1]; | |
var minor = a[2] - b[2]; | |
var patch = a[3] - b[3]; | |
return major == 0 | |
? minor === 0 | |
? patch | |
: minor | |
: major; | |
}) | |
.map(function (array) { | |
return array[0]; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment