| Title: | Simple Implementation of Semantic Versioning (SemVer) |
|---|---|
| Description: | Simple implementation of Semantic Versioning 2.0.0 ('SemVer') on the 'vctrs' package. This package provides a simple way to create, compare, and manipulate semantic versions in R. It is designed to be lightweight and easy to use. |
| Authors: | Tatsuya Shima [aut, cre] |
| Maintainer: | Tatsuya Shima <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.2.9000 |
| Built: | 2026-05-11 07:43:18 UTC |
| Source: | https://github.com/eitsupi/smvr |
smvr vectoras_smvr() is a generic function that converts an object to smvr
vector. The default method uses vctrs::vec_cast() to convert the object.
as_smvr(x, ...) ## Default S3 method: as_smvr(x, ...)as_smvr(x, ...) ## Default S3 method: as_smvr(x, ...)
x |
An object to convert to |
... |
Additional arguments passed to methods. |
A smvr class vector.
as_smvr(c("1.0.0", "2.0.0-rc.1", "3.0.0+build.1")) as_smvr(numeric_version(c("1", "2.3"))) as_smvr(NA)as_smvr(c("1.0.0", "2.0.0-rc.1", "3.0.0+build.1")) as_smvr(numeric_version(c("1", "2.3"))) as_smvr(NA)
smvr object has a specific componentThese functions check if the smvr object has a specific component.
is_pre_release(): Checks if the pre-release identifiers are present.
has_build_metadata(): Checks if the build metadata is present.
is_pre_release(x) has_build_metadata(x)is_pre_release(x) has_build_metadata(x)
x |
A smvr object. |
Indicates whether x has the specified component.
extract-component functions for extracting components from a smvr object.
v <- parse_semver(c( "1.0.0", "2.0.0-alpha", "2.0.0-beta", "2.0.0-beta.2+build.123" )) v is_pre_release(v) has_build_metadata(v)v <- parse_semver(c( "1.0.0", "2.0.0-alpha", "2.0.0-beta", "2.0.0-beta.2+build.123" )) v is_pre_release(v) has_build_metadata(v)
These functions extract the individual components of version numbers or labels, such as major, minor, patch numbers, or, pre-release identifiers and build metadata.
extract_major(x, ...) extract_minor(x, ...) extract_patch(x, ...) extract_pre_release_ids(x, ...) extract_build_metadata(x, ...) ## S3 method for class 'smvr' extract_major(x, ...) ## S3 method for class 'smvr' extract_minor(x, ...) ## S3 method for class 'smvr' extract_patch(x, ...) ## S3 method for class 'smvr' extract_pre_release_ids(x, ...) ## S3 method for class 'smvr' extract_build_metadata(x, ...) ## S3 method for class 'numeric_version' extract_major(x, ...) ## S3 method for class 'numeric_version' extract_minor(x, ...) ## S3 method for class 'numeric_version' extract_patch(x, ...)extract_major(x, ...) extract_minor(x, ...) extract_patch(x, ...) extract_pre_release_ids(x, ...) extract_build_metadata(x, ...) ## S3 method for class 'smvr' extract_major(x, ...) ## S3 method for class 'smvr' extract_minor(x, ...) ## S3 method for class 'smvr' extract_patch(x, ...) ## S3 method for class 'smvr' extract_pre_release_ids(x, ...) ## S3 method for class 'smvr' extract_build_metadata(x, ...) ## S3 method for class 'numeric_version' extract_major(x, ...) ## S3 method for class 'numeric_version' extract_minor(x, ...) ## S3 method for class 'numeric_version' extract_patch(x, ...)
x |
A version object. |
... |
Additional arguments passed to methods. |
The extracted component of the version object.
extract_major(), extract_minor(), and
extract_patch() return integer.
extract_pre_release_ids() returns pre_release_ids.
extract_build_metadata() returns character.
check-component functions for checking if smvr object has a specific component.
sem_ver <- parse_semver(c("1.2.3-alpha+001", "2.0.0", NA)) extract_major(sem_ver) extract_minor(sem_ver) extract_patch(sem_ver) extract_pre_release_ids(sem_ver) extract_build_metadata(sem_ver) # Extracting version also works for numeric_version num_ver <- numeric_version(c("1", "3.1.4.1.5", NA), strict = FALSE) extract_major(num_ver) extract_minor(num_ver) extract_patch(num_ver)sem_ver <- parse_semver(c("1.2.3-alpha+001", "2.0.0", NA)) extract_major(sem_ver) extract_minor(sem_ver) extract_patch(sem_ver) extract_pre_release_ids(sem_ver) extract_build_metadata(sem_ver) # Extracting version also works for numeric_version num_ver <- numeric_version(c("1", "3.1.4.1.5", NA), strict = FALSE) extract_major(num_ver) extract_minor(num_ver) extract_patch(num_ver)
smvr objectCheck if an object is a smvr object
is_smvr(x)is_smvr(x)
x |
An object. |
Indicates whether x is a smvr object.
is_smvr(smvr(1, 2, 3))is_smvr(smvr(1, 2, 3))
A class representing a collection of identifiers, which are used for representing pre-release versions.
There are two functions to create the pre_release_ids vector:
pre_release_ids() is a low-level constructor for creating
pre-release identifiers from individual components.
parse_pre_release_ids() parses a character vector into
pre-release identifiers.
new_pre_release_ids(...) parse_pre_release_ids(x)new_pre_release_ids(...) parse_pre_release_ids(x)
... |
< |
x |
A character vector representing pre-release identifiers.
Each identifier separated by a dot ( |
If the components are empty, they are treated as the highest precedence pre-release ids, which is used to indicate that the version is not a pre-release version.
A pre_release_ids vector.
There are some limitations on the number of identifiers in some operations:
When comparing with a string, the number of identifiers in the string. If it exceeds 5, an error is raised.
When assigning, the number of identifiers in the value being assigned. If it exceeds the number of identifiers in the target or 5, whichever is larger, an error is raised.
Please refer to the examples for details.
# Each components are concatenated with a dot new_pre_release_ids("rc", 1:3) ids <- parse_pre_release_ids( c("", "alpha.beta", "alpha.1", "beta", "beta.11", "beta.2") ) ids # Empty ids have the highest precedence # (Used to indicate not a pre-release version) vctrs::vec_sort(ids) # Can be compared with string notation ids[ids > "beta.2"] # Limitations: # 1. When comparing with a string, the number of identifiers in the string # must not exceed 5. try(ids[ids > "beta.2.3.4.5.6"]) # This works since the string is parsed first. ids[ids > parse_pre_release_ids("beta.2.3.4.5.6")] # 2. When assigning, the number of identifiers in the value being assigned # must not exceed the number of identifiers in the target or 5, # whichever is larger. try(ids[1] <- parse_pre_release_ids("beta.2.3.4.5.6"))# Each components are concatenated with a dot new_pre_release_ids("rc", 1:3) ids <- parse_pre_release_ids( c("", "alpha.beta", "alpha.1", "beta", "beta.11", "beta.2") ) ids # Empty ids have the highest precedence # (Used to indicate not a pre-release version) vctrs::vec_sort(ids) # Can be compared with string notation ids[ids > "beta.2"] # Limitations: # 1. When comparing with a string, the number of identifiers in the string # must not exceed 5. try(ids[ids > "beta.2.3.4.5.6"]) # This works since the string is parsed first. ids[ids > parse_pre_release_ids("beta.2.3.4.5.6")] # 2. When assigning, the number of identifiers in the value being assigned # must not exceed the number of identifiers in the target or 5, # whichever is larger. try(ids[1] <- parse_pre_release_ids("beta.2.3.4.5.6"))
A class representing a single pre-release identifier (alphanumeric or numeric) for Semantic Versioning 2.0.0.
new_pre_release_identifier(x = character())new_pre_release_identifier(x = character())
x |
Something that can be coerced to a character vector by
|
Identifiers are compared based on the following criteria:
If the identifier is empty, it is treated as the smallest value.
Integers greater than or equal to 0 are treated as numeric identifiers and compared numerically.
Else, identifiers are treated as alphanumeric identifiers and compared lexically ASCII sort order.
Numeric identifiers always have lower precedence than alphanumeric identifiers.
A pre_release_identifier vector.
pre_release_ids: Whole pre-release identifiers (Concatenation of pre_release_identifier).
id <- new_pre_release_identifier( c("1", "2", "10", "0a", "-1", "alpha", "beta", "", NA) ) id # empty < numeric < alphanumeric vctrs::vec_sort(id) # Works with base R vectors. id[id == "alpha" & !is.na(id)] id[id > 2L & !is.na(id)]id <- new_pre_release_identifier( c("1", "2", "10", "0a", "-1", "alpha", "beta", "", NA) ) id # empty < numeric < alphanumeric vctrs::vec_sort(id) # Works with base R vectors. id[id == "alpha" & !is.na(id)] id[id > 2L & !is.na(id)]
This is a suggested regular expression (RegEx) to check a SemVer string,
without the "^" at the start and "$" at the end.
It is useful to extract the SemVer components from VCS tags etc.
SEM_VER_PATTERNSEM_VER_PATTERN
An object of class character of length 1.
Single string
SEM_VER_PATTERN # VCS tag names often have a "v" prefix to SemVer tag_names <- c("v1.0.0", "v1.1.0-alpha.1", "v1.1.0+build.1", "not-a-version") # Extract and parse SemVer regmatches(tag_names, m = regexpr(SEM_VER_PATTERN, tag_names)) |> parse_semver()SEM_VER_PATTERN # VCS tag names often have a "v" prefix to SemVer tag_names <- c("v1.0.0", "v1.1.0-alpha.1", "v1.1.0+build.1", "not-a-version") # Extract and parse SemVer regmatches(tag_names, m = regexpr(SEM_VER_PATTERN, tag_names)) |> parse_semver()
The smvr class represents versions that follow the
Semantic Versioning Specification (SemVer).
A version number contains three components, MAJOR.MINOR.PATCH,
and optional pre-release and build metadata labels.
This is similar to the base R's numeric_version class, but always has
three components (major, minor, patch) and supports pre-release
and build metadata labels. And, unlike numeric_version,
SemVer uses dots (.) as separators and does not allow hyphens (-)
except to indicate the start of a pre-release label.
There are two functions to create smvr objects:
smvr() is a constructor from each component.
Each component must have the same length or length 1 (will be recycled).
parse_semver() parses a character vector.
smvr(major = integer(), minor = 0L, patch = 0L, pre_release = "", build = "") parse_semver(x)smvr(major = integer(), minor = 0L, patch = 0L, pre_release = "", build = "") parse_semver(x)
major, minor, patch
|
Non-negative integers representing
the major, minor, and patch version components.
The default values for |
pre_release |
Something that can be cast to a pre_release_ids vector.
This can be empty ( |
build |
Optional build metadata character vector.
Should have the pattern |
x |
A character vector representing semantic versions.
Each version should follow the
Semantic Versioning Specification.
Partial matches are not allowed (e.g., |
Build metadata is not used for ordering, but the == and != operators
check it and exactly same build metadata is required for equality.
The other operators (<, <=, >, >=) ignore build metadata.
A smvr class vector.
extract-component functions to extract components of a smvr object.
(Operations opposite to smvr()).
update-version functions to update components of a smvr object.
# SemVer versions from components smvr(4, 1:5) # Parse SemVer versions from character v <- parse_semver(c( "1.0.0", "1.0.0-alpha", "1.0.0-beta", "1.0.0-rc.2", "1.0.0-rc.10", NA )) v # Sorting vctrs::vec_sort(v) # Can be compared with string notation v[v >= "1.0.0-rc.2" & !is.na(v)] # Partial version components are treated as NA suppressWarnings(parse_semver("1.5")) # The numeric_version class supports versions with # less than 3 components, and can be cast to smvr. numeric_version("1.5") |> vctrs::vec_cast(smvr()) # Be careful with hyphens in numeric_version and SemVer. # The following examples yield opposite results. numeric_version("1.0.0-1") > "1.0.0" # 1.0.0-1 is the same as 1.0.0.1 parse_semver("1.0.0-1") > "1.0.0" # 1.0.0-1 is a pre-release version# SemVer versions from components smvr(4, 1:5) # Parse SemVer versions from character v <- parse_semver(c( "1.0.0", "1.0.0-alpha", "1.0.0-beta", "1.0.0-rc.2", "1.0.0-rc.10", NA )) v # Sorting vctrs::vec_sort(v) # Can be compared with string notation v[v >= "1.0.0-rc.2" & !is.na(v)] # Partial version components are treated as NA suppressWarnings(parse_semver("1.5")) # The numeric_version class supports versions with # less than 3 components, and can be cast to smvr. numeric_version("1.5") |> vctrs::vec_cast(smvr()) # Be careful with hyphens in numeric_version and SemVer. # The following examples yield opposite results. numeric_version("1.0.0-1") > "1.0.0" # 1.0.0-1 is the same as 1.0.0.1 parse_semver("1.0.0-1") > "1.0.0" # 1.0.0-1 is a pre-release version
These functions allows to update the components of version objects.
increment_major(), increment_minor(), and increment_patch() update
the major, minor, and patch version numbers respectively.
Note that these functions reset the pre-release and build metadata to empty.
mark_as_pre_release() marks the version as a pre-release version.
add_build_metadata() adds build metadata to the version.
increment_major(x, ...) ## S3 method for class 'smvr' increment_major(x, ...) increment_minor(x, ...) ## S3 method for class 'smvr' increment_minor(x, ...) increment_patch(x, ...) ## S3 method for class 'smvr' increment_patch(x, ...) mark_as_pre_release(x, ...) ## S3 method for class 'smvr' mark_as_pre_release(x, ids, ...) add_build_metadata(x, ...) ## S3 method for class 'smvr' add_build_metadata(x, metadata = "", ...)increment_major(x, ...) ## S3 method for class 'smvr' increment_major(x, ...) increment_minor(x, ...) ## S3 method for class 'smvr' increment_minor(x, ...) increment_patch(x, ...) ## S3 method for class 'smvr' increment_patch(x, ...) mark_as_pre_release(x, ...) ## S3 method for class 'smvr' mark_as_pre_release(x, ids, ...) add_build_metadata(x, ...) ## S3 method for class 'smvr' add_build_metadata(x, metadata = "", ...)
x |
An version object |
... |
Additional arguments passed to methods. |
ids |
Something can be cast to pre_release_ids representing the
pre-release identifiers, length must be 1 or the same as |
metadata |
A character vector of build metadata,
length must be 1 or the same as |
An updated version object with the specified changes applied.
v <- parse_semver(c("0.9.9", "1.0.0-a.1", "1.1.0+1")) increment_major(v) increment_minor(v) increment_patch(v) mark_as_pre_release(v, ids = "rc.1") add_build_metadata(v, metadata = "build.1")v <- parse_semver(c("0.9.9", "1.0.0-a.1", "1.1.0+1")) increment_major(v) increment_minor(v) increment_patch(v) mark_as_pre_release(v, ids = "rc.1") add_build_metadata(v, metadata = "build.1")