semver

Semantic Versioning (SemVer 2.0.0) parser, comparator, bumper and range matcher.

v1.0.0Application & logicbundledimport "semver";
Overview Installation Documentation Examples

Overview

A zero-dependency Semantic Versioning (SemVer 2.0.0) library for HolyPHP. Provides strict version parsing, precedence comparison, version bumping (major, minor, patch), and range matching (^, ~, >=, <=, >, <, =, and ||).

bundled — works offline zero dependencies version range

Quick start

import "semver";

$v = semver_parse("1.2.3-beta.1+build.42");
println($v->major);      // 1
println($v->minor);      // 2
println($v->patch);      // 3
println($v->prerelease); // beta.1
println($v->build);      // build.42

$next = $v->bumpMinor();
println($next->toString()); // 1.3.0

println(semver_compare("1.0.0-alpha", "1.0.0")); // -1
println(semver_satisfies("1.4.2", "^1.2.0"));    // true
println(semver_valid("2.0.0"));                  // true

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install semver

This adds the library to hphp.yaml, installs it into .hphp/packages/semver/ and pins the exact version in hphp.lock:

require:
  semver: "^1.0"

Use it

import "semver";

semver is bundled with the compiler, so import "semver"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install semver          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install semver
Packagesemver
Version1.0.0 (follows the compiler)
Entry filesemver.hphp
Sourcepackages/lib/semver ↗

Documentation

API Reference

class SemVer

Represents a parsed SemVer 2.0.0 version.

Properties

Constructor

Methods

Functions

Supported Range Syntax

Error Handling

All invalid inputs throw Exception with a clear, actionable message prefixed with semver::

Examples

01Quick start

Bundled with the compiler — import "semver"; works offline in any file, or add it to a project with hphp install semver.

import "semver";

$v = semver_parse("1.2.3-beta.1+build.42");
println($v->major);      // 1
println($v->minor);      // 2
println($v->patch);      // 3
println($v->prerelease); // beta.1
println($v->build);      // build.42

$next = $v->bumpMinor();
println($next->toString()); // 1.3.0

println(semver_compare("1.0.0-alpha", "1.0.0")); // -1
println(semver_satisfies("1.4.2", "^1.2.0"));    // true
println(semver_valid("2.0.0"));                  // true
Try it
hphp install semver
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_semver.hphp.