Version
Object → Version
A semantic version object.
Constructor
Version(major: integer?, minor: integer?, patch: integer?, pre_release: string?, build: string?): VersionFields
| Name | Type | Description |
|---|---|---|
major | integer | The major version number. (0-1023) |
minor | integer | The minor version number. (0-2047) |
patch | integer | The patch version number. (0-2047) |
pre_release | string | The pre-release identifiers, empty string if none. |
build | string | The build metadata, empty string if none. |
Static functions
add | Adds a version to the current version. |
copy | Creates a copy of the version. |
fromString | Gets a version object from a version string. |
select | Returns the first version that satisfies the range selector. |
validate | Checks if the string is a valid semantic version. |
#Version.add
function Version.add(self: Version, major: integer | nil, minor: integer | nil, patch: integer | nil, pre_release: integer | nil): Version
Adds a version to the current version.
Parameters
| Name | Type | Description |
|---|---|---|
self | Version | The version. |
major | integer | nil | The major version. |
minor | integer | nil | The minor version. |
patch | integer | nil | The patch version. |
pre_release | integer | nil | The value to add to the last numeric pre-release identifier. |
Returns
| Type | Name | Description |
|---|---|---|
Version | version_obj | The current version. |
#Version.copy
function Version.copy(self: Version): Version
Creates a copy of the version.
Parameters
| Name | Type | Description |
|---|---|---|
self | Version | The version. |
Returns
| Type | Name | Description |
|---|---|---|
Version | version_obj | The new version. |
#Version.fromString
function Version.fromString(str: string): Version
Gets a version object from a version string.
The string must be a valid semantic version, an optional v prefix is allowed.
Parameters
| Name | Type | Description |
|---|---|---|
str | string | The version string in the format major.minor.patch[-pre_release][+build]. |
Returns
| Type | Name | Description |
|---|---|---|
Version | version_obj | The version object. |
#Version.select
function Version.select(versions: Version[], selector: string): Version | nil, integer
Returns the first version that satisfies the range selector.
Parameters
| Name | Type | Description |
|---|---|---|
versions | Version[] | The versions to search. |
selector | string | The range selector. |
Returns
| Type | Name | Description |
|---|---|---|
Version | nil | matched_version | The matched version, or nil if not found. |
integer | version_position | The position of the matched version, or -1 if not found. |
#Version.validate
function Version.validate(str: string): boolean
Checks if the string is a valid semantic version.
Parameters
| Name | Type | Description |
|---|---|---|
str | string | The version string. |
Returns
| Type | Name | Description |
|---|---|---|
boolean | is_valid | true if the string is a valid version, false otherwise. |
Methods
compare | Compares two versions by precedence, build metadata is ignored. |
match | Checks if the version satisfies the range selector. |
sub | Subtracts a version from the current version. |
#Version:compare
function Version:compare(version_obj: Version): integer
Compares two versions by precedence, build metadata is ignored.
Parameters
| Name | Type | Description |
|---|---|---|
version_obj | Version | The version to compare with. |
Returns
| Type | Name | Description |
|---|---|---|
integer | comparison | -1 if the current version is lower, 1 if higher, 0 if equal. |
#Version:match
function Version:match(selector: string): boolean
Checks if the version satisfies the range selector.
Supports npm-style ranges: 1.2.3, >=1.2.3 <2.0.0, 1.2.x, ~1.2.3, ^1.2.3, 1.2.3 - 2.3.4, 1.x || >=2.5.0.
Parameters
| Name | Type | Description |
|---|---|---|
selector | string | The range selector. |
Returns
| Type | Name | Description |
|---|---|---|
boolean | is_matched | true if the version satisfies the range, false otherwise. |
#Version:sub
function Version:sub(major: integer | nil, minor: integer | nil, patch: integer | nil, pre_release: integer | nil): Version
Subtracts a version from the current version.
Parameters
| Name | Type | Description |
|---|---|---|
major | integer | nil | The major version. |
minor | integer | nil | The minor version. |
patch | integer | nil | The patch version. |
pre_release | integer | nil | The value to subtract from the last numeric pre-release identifier. |
Returns
| Type | Name | Description |
|---|---|---|
Version | version_obj | The current version. |