Giter Site home page Giter Site logo

semver's Introduction

Build Status Go Report Card GoDoc

Go Semantic Versioning Parser

Basic Usage

package main

import (
  "fmt"
  "os"

  "github.com/juamedgod/semver"
)

func main() {
  if result, err := semver.Satisfies("1.3.4", "1.2.1 - 1.4.0"); err != nil {
    fmt.Fprintf(os.Stderr, "Error evalutating semver expression: %v", err)
    os.Exit(1)
  } else {
    fmt.Println(result)
  }
}

Versions

A Version object defines a semver version.

Versions can be created:

// err1 should be nil, as the version is properly formated
v1, err1 := ParseVersion("1.3.5")

// err2 will contain an error
v2, err := ParseVersion("asdf")

// If you are confident your version string will validate
// you can use the "Must" version, which will panic on error but 
// will make the function more convenient

v3 := MustParseVersion("5.4")

// You can manually define them
v5 := &Version{Major: 6, Minor: 0, Patch: 4}

And compared

// true
v3.Greater(v2)
 
// false
v2.Greater(v3) 

// true
v3.GreaterOrEqual(v3)

// true
v3.Less(v5)

// true
v5.LessOrEqual(v3)

// true
v5.Equal(v5)

Ranges

A Range defines a range of versions. The syntax to create them is similar to the one used in Versions

// err1 should be nil, as the range is properly formated
r1, err1 := ParseRange(">1.3.5")

// err2 will contain an error
r2, err := ParseRange("asdf")

// If you are confident your range string will validate
// you can use the "Must" version, which will panic on error but 
// will make the function more convenient

r3 := MustParseRange("^5.4")

And can be used to check if a certain Version fulfills it:

v1 := MustParseVersion("1.0.4")
v2 := MustParseVersion("6.4.3")

r1 := MustParseRange(">1.3.5")
r2 := MustParseRange("< 4.0")

// false
r1.Contains(v1)

// true
r2.Contains(v1)

// true
r1.Contains(v2)

// false
r2.Contains(v2)

Expressions

An Expression is a combination of ranges. Ranges separated by spaces act as an "AND" operation, and those serparated by || act as an "OR".

Creating expressions follow the usual procedure:

// err1 should be nil, as the range is properly formated
e1, err1 := ParseExpr(">2.3.1 <3.4.1")

// err2 will contain an error
e2, err2 :=  ParseExpr("fooo")

// If you are confident your range string will validate
// you can use the "Must" version, which will panic on error but 
// will make the function more convenient
e3 := MustParseExpr("<1.5 || >5.4")

And can then be used to check if a version matches:

v1 := MustParseVersion("3.1.0")
v2 := MustParseVersion("1.1.0")
v3 := MustParseVersion("6.1.0")

e1 := MustParseExpr(">2.3.1 <3.4.1")

// true
e1.Matches(v1)
// false
e1.Matches(v2)
// false
e1.Matches(v3)

e2 := MustParseExpr("<1.5 || >5.4")
// false
e2.Matches(v1)
// true
e2.Matches(v2)
// true
e2.Matches(v3)

semver's People

Contributors

juamedgod avatar

Stargazers

Tobias Salzmann avatar Jesus Lopez de la Cruz avatar Yota Toyama avatar Michael Wagner avatar Masaki ISHIYAMA avatar Koichi Shiraishi avatar Jorge Marin avatar Jose Fuentes Castillo avatar Fernando avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.