semver

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2020 License: MIT Imports: 1 Imported by: 9

README

SemVer

GoDoc Go Report Card

This semver package provides the ability to sort & compare Semantic Versions in Go.

This package follows Semantic Versioning 2.0.0 with one exception: It recognizes MAJOR and MAJOR.MINOR (with no prereleases or build suffixes) as shorthands for MAJOR.0.0 and MAJOR.MINOR.0.

It is based on Golang's internal semver functionality, and has the following features:

  • Compare semantic versions, including support for prereleases
  • Sort semantic versions (low to high, high to low)
  • Ignores any v prefix
  • See docs for more features

If you require a full-blown solution then try masterminds/semver.

Usage

if semver.Compare("1.0.1", "1.0.0") == 1 { // higher
  //...
}

if semver.Compare("1.0.1", "1.0.1-beta") == 1 { // higher
  //...
}

fmt.Println(semver.Max("v1.0.1", "v1.0.1-beta"))
// v1.0.1

versions := []string{"5.0.0", "v5.1.0", "5.1.0-beta", "1.2.3"}
fmt.Prinlnln(semver.SortMax(versions))
// [v5.1.0 5.1.0-beta 5.0.0 1.2.3]

Documentation

Overview

Package semver implements comparison of semantic version strings. In this package, semantic version strings may begin with a leading "v", as in "v1.0.0", or "1.0.0".

The general form of a semantic version string accepted by this package is MAJOR[.MINOR[.PATCH[-PRERELEASE][+BUILD]]]

where square brackets indicate optional parts of the syntax; MAJOR, MINOR, and PATCH are decimal integers without extra leading zeros; PRERELEASE and BUILD are each a series of non-empty dot-separated identifiers using only alphanumeric characters and hyphens; and all-numeric PRERELEASE identifiers must not have leading zeros.

This package follows Semantic Versioning 2.0.0 (see semver.org) with one exception. It recognizes MAJOR and MAJOR.MINOR (with no prerelease or build suffixes) as shorthands for MAJOR.0.0 and MAJOR.MINOR.0.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build(v string) string

Build returns the build suffix of the semantic version v. For example, Build("v2.1.0+meta") == "+meta". If v is an invalid semantic version string, Build returns the empty string.

func Compare

func Compare(v, w string) int

Compare returns an integer comparing two versions according to according to semantic version precedence. The result will be 0 if v == w, -1 if v < w, or +1 if v > w.

An invalid semantic version string is considered less than a valid one. All invalid semantic version strings compare equal to each other.

func IsValid

func IsValid(v string) bool

IsValid reports whether v is a valid semantic version string.

func Major

func Major(v string) string

Major returns the major version prefix of the semantic version v. For example, Major("v2.1.0") == "2". If v is an invalid semantic version string, Major returns the empty string.

func MajorMinor

func MajorMinor(v string) string

MajorMinor returns the major.minor version prefix of the semantic version v. For example, MajorMinor("v2.1.0") == "2.1". If v is an invalid semantic version string, MajorMinor returns the empty string.

func Max

func Max(v, w string) string

Max returns the version string that compares greater.

func Minor

func Minor(v string) string

Minor returns the minor version prefix of the semantic version v. For example, Minor("v2.1.0") == "1". If v is an invalid semantic version string, Minor returns the empty string.

func Patch

func Patch(v string) string

Patch returns the patch version prefix of the semantic version v. For example, Patch("v3.2.1") == "1". If v is an invalid semantic version string, Patch returns the empty string.

func Prerelease

func Prerelease(v string) string

Prerelease returns the prerelease suffix of the semantic version v. For example, Prerelease("v2.1.0-pre+meta") == "-pre". If v is an invalid semantic version string, Prerelease returns the empty string.

func SortMax

func SortMax(versions []string) []string

SortMax returns the versions in order of their version (descending). It skips (ignores) invalid semversions.

func SortMin

func SortMin(versions []string) []string

SortMin returns the versions in order of their version (ascending). It skips (ignores) invalid semversions.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL