Skip to contents

The smvr class represents versions that follow the Semantic Versioning Specification (SemVer).

  • smvr() is a constructor for creating smvr objects from each component.

  • parse_semver() parses a character vector into smvr objects.

Usage

parse_semver(x)

smvr(major = integer(), minor = 0L, patch = 0L, pre_release = "", build = "")

Arguments

x

A character vector representing semantic versions. Each version should follow the Semantic Versioning Specification. Partial matches are not allowed (e.g., "1.0" is not valid).

major, minor, patch

Non-negative integers representing the major, minor, and patch version components. The default values for minor and patch are 0.

pre_release

Something that can be cast to a pre_release_ids vector. This represents pre-release identifiers, which can be empty ("") meaning non pre-release.

build

Optional build metadata character vector. Should have the pattern ^[a-zA-Z0-9-]+ and can contain multiple components separated by dots ("."). This can be empty ("") meaning no build metadata.

Value

A smvr class vector.

Details

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.

Examples

# SemVer versions from components
smvr(4, 1:5)
#> <smvr[5]>
#> [1] 4.1.0 4.2.0 4.3.0 4.4.0 4.5.0

# Parse SemVer versions from character
parse_semver(c("1.0.0-alpha", "1.0.0-beta+exp.sha.5114f85"))
#> <smvr[2]>
#> [1] 1.0.0-alpha                1.0.0-beta+exp.sha.5114f85

v <- parse_semver(c(
  "1.0.0",
  "1.0.0-alpha",
  "1.0.0-beta",
  "1.0.0-rc.1",
  "1.0.0-rc.2",
  NA
))
v
#> <smvr[6]>
#> [1] 1.0.0       1.0.0-alpha 1.0.0-beta  1.0.0-rc.1  1.0.0-rc.2  <NA>       

# Sorting
vctrs::vec_sort(v)
#> <smvr[6]>
#> [1] 1.0.0-alpha 1.0.0-beta  1.0.0-rc.1  1.0.0-rc.2  1.0.0       <NA>       

# Works with base R vectors.
v[v >= "1.0.0-rc.2"]
#> <smvr[3]>
#> [1] 1.0.0      1.0.0-rc.2 <NA>      

# Partial version components are treated as NA
suppressWarnings(parse_semver("1.5"))
#> <smvr[1]>
#> [1] <NA>

# 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())
#> <smvr[1]>
#> [1] 1.5.0