GetSemver
Parses and returns a Semver object version of the passed version string or if you do not pass in a version, we will return to you a Semver builder {@link Semver#of()} object, so you can programmaticaly build a version object.
Semver is a tool that provides useful methods to manipulate versions that follow the "semantic versioning" specification (see semver.org and github).
Some of the methods provided by Semver are:
{@link Semver#compareTo(Semver)}: Compares two versions and returns -1, 0, or 1 if the first version is less than, equal to, or greater than the second version, respectively.
{@link Semver#satisfies(String)}: Checks if the version satisfies the given range.
{@link Semver#isValid(String)}: Checks if the version is valid.
{@link Semver#nextMajor()}: Increments the major version.
{@link Semver#nextMinor()}: Increments the minor version.
{@link Semver#nextPatch()}: Increments the patch version.
{@link Semver#withBuild(String)}: Set the build version.
{@link Semver#withPreRelease(String)}: Set the pre-release version.
{@link Semver#isStable()}: Checks if the version is stable.
Here are some examples of how to parse and manipulate versions using Semver:
var version = GetSemver( "1.2.3-alpha+20151212" );
var version = GetSemver( "1.2.3-alpha" );
var version = GetSemver( "1.2.3" );
var version = GetSemver( "1.2.3+20151212" );
var version = GetSemver( "1.2.3-alpha.1" );
var version = GetSemver( "1.2.3-alpha.beta" );
Here are some examples of comparing versions using Semver:
var version1 = GetSemver( "1.2.3" );
var version2 = GetSemver( "1.2.4" );
var version3 = GetSemver( "1.3.0" );
version1.compare( version2 ); // -1
version1.compare( version3 ); // -1
version2.compare( version3 ); // -1
To use the builder you can do something like this:
var version = GetSemver().withMajor( 1 ).withMinor( 2 ).withPatch( 3 ).withPreRelease( "alpha" ).toSemver();
var versionString = GetSemver().withMajor( 1 ).withMinor( 2 ).withPatch( 3 ).withPreRelease( "alpha" ).toString();
Method Signature
GetSemver(version=[string])
Arguments
version
string
false
The version string to parse.
Examples
Related
Last updated
Was this helpful?