Version

Represents an immutable MAJOR.MINOR.PATCH version following semantic versioning rules.

Constructor

new Version(version: { major: number, minor: number, patch: number, preRelease: string, build: string })
new Version(version: string)
new Version(major?: number, minor?: number, patch?: number, preRelease?: string, build?: string)
new Version()

Parses a version out of another version, a semantic versioning string, or individual major.minor.patch components.

An empty constructor call will result in a valid 0.0.0 version.

Public Properties

major The major component of the version. Increments whenever incompatible API changes are introduced, with the exception of 0.x.y, which is for initial development and may see breaking changes at any time.
minor The minor component of the version. Increments whenever backwards- compatible functionality is added.
patch The patch component of the version. Increments whenever backwards- compatible bug fixes are introduced.
preRelease The pre-release component of the version. Is separated from the other components with a dash (-) and clearly distinguishes the version from a “normal” version (e.g. an alpha version), marking it as potentially unstable.
build The build metadata of the version. Is separated from the other components with a plus (+) and can contain additional information about the specific build.

Public Computed Properties

valid Returns a value indicating whether this Version instance represents a valid semantic version. If this value is false, all components of the instance are either NaN or an empty string.

Public Non-Mutator Methods

incrementMajor(): Version

Returns a new Version with the major component incremented by 1. Note that this will strip out any preRelease or build labels and reset the patch and minor components.

If the version is invalid, the new instance will also be invalid.

incrementMinor(): Version

Returns a new Version with the minor component incremented by 1. Note that this will strip out any preRelease or build labels and reset the patch component.

If the version is invalid, the new instance will also be invalid.

incrementPatch(): Version

Returns a new Version with the patch component incremented by 1. Note that this will strip out any preRelease or build labels.

If the version is invalid, the new instance will also be invalid.

equals(version: Version | string): boolean

Returns true if the two versions are completely identical.

Note that two invalid versions are always identical.

toString(): string

Returns the proper semantic string representation of this semantic version.

Note that invalid versions return "Invalid Version".

toJSON(): string

Same output as `toString()`.

compare(version: Version | string): CompareResult

Compares this version with another one, returning a result based on which should take precedence over the other.

If this version is lower than the passed version in precedence, returns CompareResult.Less (-1).
If this version is equal to the passed version in precedence, returns CompareResult.Equal (0).
If this version is greater than the passed version in precedence, returns CompareResult.Greater (1).
If this version is invalid but the passed version is not, returns CompareResult.Less (-1).

A pre-release version is always lower in precedence. If both versions have a pre-release component, each dot-separated identifier is compared numerically or lexically, whichever applies. If the number of identifiers is not equal and all preceding identifiers are the same, the version with more identifiers wins.

Note that a result of 0 (or CompareResult.Equal) does not mean both instances are identical, just that none take precedence over the other. Use equals() to check for equality.

Remarks

This class is not capable of representing versions with individual components larger than Number.MAX_SAFE_INTEGER. Versions constructed with such large numbers are not inherently invalid, but their version components will be rounded to the next representable integer value.