bare-semver
Minimal semantic versioning library for Bare
bare-semver — Minimal semantic versioning library for Bare.
npm i bare-semverUsage
const semver = require('bare-semver')
const version = semver.Version.parse('1.2.3-alpha.1+build.42')
console.log(version.major) // 1
console.log(version.minor) // 2
console.log(version.patch) // 3
const satisfied = semver.satisfies('1.2.3', '>=1.0.0 <2.0.0')
console.log(satisfied) // trueAPI
errors
errors.INVALID_RANGE(msg: string, fn?: Function): SemVerError
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
fn? | Function | — | Optional function to omit from the top of the generated stack trace, passed to Error.captureStackTrace. |
Returns SemVerError — A SemVerError with code set to 'INVALID_RANGE', for the caller to throw.
errors.INVALID_VERSION(msg: string, fn?: Function): SemVerError
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
fn? | Function | — | Optional function to omit from the top of the generated stack trace, passed to Error.captureStackTrace. |
Returns SemVerError — A SemVerError with code set to 'INVALID_VERSION', for the caller to throw.
Version
Version
new Version(major: number, minor: number, patch: number, opts?: { prerelease?: string[]; build?: string[] })Create a new version with the given major, minor, and patch components.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
major | number | — | The major version number. |
minor | number | — | The minor version number. |
patch | number | — | The patch version number. |
opts? | { prerelease?: string[]; build?: string[] } | — | Optional prerelease and build tag arrays; each defaults to an empty array. |
build: string[]
Source
An array of build metadata tags.
compare(version: Version): boolean
Source
Compare version with other, returning 1 if version is greater, -1 if less, or 0 if equal. Comparison follows the Semantic Versioning 2.0.0 specification, including prerelease precedence rules.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
version | Version | — | The version to compare against. |
major: number
Source
The major version number.
minor: number
Source
The minor version number.
patch: number
Source
The patch version number.
prerelease: string[]
Source
An array of prerelease tags.
Version.toString(): string
Source
Return the string representation of version.
Version.compare(a: Version, b: Version): number
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
a | Version | — | — |
b | Version | — | — |
Version.parse(input: string): Version
Source
Parse a semantic version string into a Version instance.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
input | string | — | The version string to parse. |
Throws
INVALID_VERSION—inputis not a valid version string.
Comparator
new Comparator(operator: number, version: Version)
Source
Create a new comparator with the given operator constant and version.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
operator | number | — | One of the constants operator values (EQ, LT, LTE, GT, GTE). |
version | Version | — | The version the comparator matches against. |
operator: number
Source
The comparison operator constant.
Comparator.test(version: Version): boolean
Source
Test whether version satisfies the comparator.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
version | Version | — | The version to test against the comparator. |
Returns boolean — true if version satisfies the comparator's operator and version, false otherwise.
Comparator.toString(): string
Source
Return the string representation of the comparator (operator and version), e.g. >=1.2.3.
version: Version
Source
The Version instance to compare against.
Range
new Range(comparators?: Comparator[][])
Source
Create a new range from a two-dimensional array of Comparator instances. Each inner array represents a set of comparators joined by intersection, and the outer array represents the union of those sets.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
comparators? | Comparator[][] | — | Two-dimensional array of comparator sets: the outer array is a union (OR) of inner arrays, each an intersection (AND); defaults to an empty range that matches nothing. |
comparators: Comparator[][]
Source
The two-dimensional array of Comparator instances.
Range.parse(input: string): Range
Source
Parse a range string into a Range instance. Supports comparison operators (<, <=, >, >=, =), partial versions, and logical OR (||).
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
input | string | — | The range string to parse. |
Throws
INVALID_VERSION—inputis not valid range syntax (reported via theINVALID_VERSIONcode —INVALID_RANGEis not currently thrown by the parser).
Range.test(version: Version): boolean
Source
Test whether version satisfies the range.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
version | Version | — | The version to test against the range. |
Returns boolean — true if version satisfies any comparator set in the range, false otherwise.
Range.toString(): string
Source
Return the string representation of the range, e.g. >=1.2.3 <2.0.0.
Functions
satisfies(version: Version, range: Range): boolean
Source
Test whether version satisfies range. Both version and range may be strings, in which case they will be parsed.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
version | Version | — | The version to test, or a version string to parse. |
range | Range | — | The range to test against, or a range string to parse. |
Returns boolean — true if version matches range, false otherwise.
Throws
INVALID_VERSION—versionorrangeis a string that fails to parse.
Constants and variables
constants: { EQ: 1; LT: 2; LTE: 3; GT: 4; GTE: 5 }
Source
An object containing the comparison operator constants.
See also
- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.