Swiftpack.co - ddddxxx/Semver as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by ddddxxx.
ddddxxx/Semver v0.2.1
Swift Semantic Versioning library
⭐️ 22
🕓 3 years ago
.package(url: "https://github.com/ddddxxx/Semver.git", from: "v0.2.1")

Semver

Github CI Status supports platforms

Semver is a Swift implementation of the Semantic Versioning.

Requirements

  • Swift 5.0+

Usage

Quick Start

import Semver

// A leading "v" character is ignored.
let version = Semver("v1.3.8-rc.1+build.3")!

version > Semver("1.0.2+39f1d74")! // true

Equality

The Equatable conformance respect Semver semantic equality and ignore build metadata. This also affect Comparable and Hashable.

You can use === and !== to take build metadata into account.

let v1 = Version("1.0.0+100")!
let v2 = Version("1.0.0+200")!

v1 == v2 // true
v1 <= v2 // true
v1.hashValue == v2.hashValue // true
Set([v1, v2]).count == 1 // ❗️true

v1 === v2 // false
v1 !== v2 // true

Validity Check

The string initializer Semver.init?(_:) always produce valid version (or nil). The literal initializer Semver.init(stringLiteral:) only accept StaticString and crash when failed.

However, the member wise initializer Semver.init(major:minor:patch:prerelease:buildMetadata:) doesn't perform validity checks on its fields. It's possible to form an invalid version. You can manually validate a version using Semver.isValid.

let version = Semver(major: 0, minor: 0, patch: -1) // invalid version 0.0.-1
version.isValid // false

Installation

Swift Package Manager

Add the project to your Package.swift file:

package.dependencies += [
    .package(url: "https://github.com/ddddxxx/Semver", .upToNextMinor("0.2.0"))
]

Carthage

Add the project to your Cartfile:

github "ddddxxx/Semver"

Copy File

This is a lightweight library contains only one file. You can simply copy/paste the Semver file into your project.

License

Semver is available under the MIT license. See the LICENSE file.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics