Reference
Classes
Comparator
Defined in: comparator.ts:120
Class used to parse the || separated portions
of a range, and evaluate versions against it.
This does most of the heavy lifting of range testing, and provides little affordance for improperly formatted strings. It should be considered an internal class, and usually not accessed directly.
Constructors
new Comparator()
new Comparator(comp, includePrerelease): ComparatorDefined in: comparator.ts:157
Parameters
| Parameter | Type | Default value |
|---|---|---|
comp | string | undefined |
includePrerelease | boolean | false |
Returns
Properties
| Property | Type | Default value | Description |
|---|---|---|---|
includePrerelease | boolean | undefined | does this range include prereleases, even when they do not match the tuple in the comparator? |
isAny | boolean | false | true if this comparator is a '*' type of range. Note that it still will not match versions with a prerelease value, unless the tuple in the version matches the tuple provided to the comparator, and the comparator version also has a prerelease value, unless includePrerelease is set. |
isNone | boolean | false | true if this comparator can not match anything |
raw | string | undefined | raw string used to create this comparator |
tokens | string[] | undefined | tokens extracted from the raw string input |
tuples | ( | Comparator | OVTuple)[] | [] | Either the any comparator, the none comparator, or an operator and a ParsedXRange |
Methods
test()
test(v): booleanDefined in: comparator.ts:630
return true if the version is a match for this comparator
Parameters
| Parameter | Type |
|---|---|
v | Version |
Returns
boolean
toString()
toString(): stringDefined in: comparator.ts:148
the canonical strict simplified parsed form of this constructor
Returns
string
Range
Defined in: range.ts:25
A representation of a semver range, used to test versions.
Includes a set of comparators representing the ||-separated
sections of the range string
Constructors
new Range()
new Range(range, includePrerelease): RangeDefined in: range.ts:50
Parameters
| Parameter | Type | Default value |
|---|---|---|
range | string | undefined |
includePrerelease | boolean | false |
Returns
Properties
| Property | Type | Default value | Description |
|---|---|---|---|
includePrerelease | boolean | undefined | true if all prerelease versions should be included |
isAny | boolean | undefined | true if the range is * |
isSingle | boolean | undefined | true if the range is a single semver version |
raw | string | undefined | raw string used to create this Range |
set | Comparator[] | [] | set of Comparator objects representing the ` |
Methods
test()
test(v): booleanDefined in: range.ts:100
test a Version against the range
Parameters
| Parameter | Type |
|---|---|
v | Version |
Returns
boolean
toString()
toString(): stringDefined in: range.ts:105
return the simplified canonical form of this range
Returns
string
Version
Defined in: version.ts:71
A parsed object representation of a SemVer version string
This is a bit less forgiving than node-semver, in that prerelease versions MUST start with '-'. Otherwise, the allowed syntax is identical.
Constructors
new Version()
new Version(
version,
major,
minor,
patch,
prerelease,
build): VersionDefined in: version.ts:145
Parameters
| Parameter | Type |
|---|---|
version | string |
major | number |
minor | number |
patch | number |
prerelease | undefined | string |
build | undefined | string |
Returns
Properties
| Property | Type | Description |
|---|---|---|
build? | string[] | List of '.'-separated strings in the build section. This is undefined if the version does not have a build. |
major | number | major version number |
minor | number | minor version number |
patch | number | patch version number |
prerelease? | (string | number)[] | List of '.'-separated strings and numbers indicating that this version is a prerelease. This is undefined if the version does not have a prerelease section. |
raw | string | raw string provided to create this Version |
Methods
compare()
compare(v): -1 | 0 | 1Defined in: version.ts:196
Return 1 if this is > the provided version, -1 if we're less, or 0 if they are equal.
No special handling for prerelease versions, this is just a precedence comparison.
This can be used to sort a list of versions by precedence:
const versions: Version[] = getVersionsSomehow()
const sorted = versions.sort((a, b) => a.compare(b))Parameters
| Parameter | Type |
|---|---|
v | Version |
Returns
-1 | 0 | 1
equals()
equals(v): booleanDefined in: version.ts:257
true if these two versions have equal SemVer precedence
Parameters
| Parameter | Type |
|---|---|
v | Version |
Returns
boolean
greaterThan()
greaterThan(v): booleanDefined in: version.ts:237
true if this version is > the argument
Parameters
| Parameter | Type |
|---|---|
v | Version |
Returns
boolean
greaterThanEqual()
greaterThanEqual(v): booleanDefined in: version.ts:242
true if this version is >= the argument
Parameters
| Parameter | Type |
|---|---|
v | Version |
Returns
boolean
inc()
inc(part, prereleaseIdentifier?): VersionDefined in: version.ts:329
Increment the version in place, in the manner specified.
Part behaviors:
-
'major'If the version is aM.0.0-...version with a prerelease, then simply drop the prerelease. Otherwise, set the minor and patch to 0, and increment the major. So1.0.0-betabecomes1.0.0, and1.2.3becomes2.0.0 -
'minor'If the version is aM.m.0-...version with a prerelease, then simply drop the prerelease. Otherwise, set the patch to 0, and increment the minor. So1.2.0-betabecomes1.2.0, and1.2.3becomes1.3.0. -
'patch'If the version has a prerelease, then simply drop the prerelease. Otherwise, increment the patch value. So1.2.3-betabecomes1.2.3and1.2.3becomes1.2.4. -
'premajor'Set the patch and minor versions to0, increment the major version, and add a prerelease, using the optional identifier. -
'preminor'Set the patch version to0, increment the minor version, and add a prerelease, using the optional identifier. -
'prepatch'If a prerelease is already present, increment the patch version, otherwise leave it untouched, and add a prerelease, using the optional identifier. -
'prerelease'If a prerelease version is present, then behave the same as'prepatch'. Otherwise, add a prerelease, using the optional identifier. -
'pre'This is mostly for use by the other prerelease incrementers.-
If a prerelease identifier is provided:
Update that named portion of the prerelease. For example,
inc('1.2.3-beta.4', 'pre', 'beta')would result in1.2.3-beta.5.If there is no prerelease identifier by that name, then replace the prerelease with
[name]. Soinc('1.2.3-alpha.4', 'pre', 'beta')would result in1.2.3-beta.If the prerelease identifer is present, but has no numeric value following it, then add
0. Soinc('1.2.3-beta', 'pre', 'beta')would result in1.2.3-beta.0. -
If no prerelease identifier is provided:
If there is no current prerelease, then set the prerelease to
0. So,inc('1.2.3', 'pre')becomes1.2.3-0.If the last item in the prerelease is numeric, then increment it. So,
inc('1.2.3-beta.3', 'pre')becomes1.2.3-beta.4.
-
Parameters
| Parameter | Type |
|---|---|
part | | "major" | "minor" | "patch" | "pre" | "premajor" | "preminor" | "prepatch" | "prerelease" |
prereleaseIdentifier? | string |
Returns
lessThan()
lessThan(v): booleanDefined in: version.ts:247
true if this version is < the argument
Parameters
| Parameter | Type |
|---|---|
v | Version |
Returns
boolean
lessThanEqual()
lessThanEqual(v): booleanDefined in: version.ts:252
true if this version is <= the argument
Parameters
| Parameter | Type |
|---|---|
v | Version |
Returns
boolean
rcompare()
rcompare(v): numberDefined in: version.ts:232
The inverse of compare, for sorting version lists in reverse order
Parameters
| Parameter | Type |
|---|---|
v | Version |
Returns
number
satisfies()
satisfies(r): booleanDefined in: version.ts:271
true if this version satisfies the range
Parameters
| Parameter | Type |
|---|---|
r | Range |
Returns
boolean
toString()
toString(): stringDefined in: version.ts:96
Canonical strict form of this version
Returns
string
tupleEquals()
tupleEquals(v): booleanDefined in: version.ts:262
just compare the M.m.p parts of the version
Parameters
| Parameter | Type |
|---|---|
v | Version |
Returns
boolean
parse()
static parse(version): VersionDefined in: version.ts:103
Generate a Version object from a SemVer string
Parameters
| Parameter | Type |
|---|---|
version | string |
Returns
Type Aliases
ComplexOperator
type ComplexOperator = "^" | "~" | "~>";Defined in: comparator.ts:10
operators that are expanded to simpler forms
IncrementType
type IncrementType = typeof versionIncrements[number];Defined in: version.ts:63
Types of incrementing supported by Version#inc
OVTuple
type OVTuple = [SimpleOperator, Version];Defined in: comparator.ts:26
comparator expressed as a [operator,version] tuple
ParsedXMajor
type ParsedXMajor = [];Defined in: comparator.ts:82
a ParsedXRange that is just a *
ParsedXMinor
type ParsedXMinor = [number];Defined in: comparator.ts:86
a ParsedXRange that is just a major version
ParsedXPatch
type ParsedXPatch = [number, number];Defined in: comparator.ts:90
a ParsedXRange that is just a major and minor version
ParsedXRange
type ParsedXRange =
| ParsedXMajor
| ParsedXMinor
| ParsedXPatch
| ParsedXVersion;Defined in: comparator.ts:77
The result of parsing a version value that might be either a full
version like 1.2.3 or an X-Range like 1.2.x
ParsedXVersion
type ParsedXVersion = [number, number, number, string | undefined, string | undefined];Defined in: comparator.ts:94
a ParsedXRange that is a full version
SimpleOperator
type SimpleOperator = "" | "<" | "<=" | ">" | ">=";Defined in: comparator.ts:8
all comparators are expressed in terms of these operators
Variables
versionIncrements
const versionIncrements: readonly ["major", "minor", "patch", "pre", "premajor", "preminor", "prepatch", "prerelease"];Defined in: version.ts:49
Values of valid increment types.
Functions
build()
function build(version): undefined | string[]Defined in: index.ts:441
extract the list of build identifiers, or undefined if the version
is invalid. If no build identifiers are present, returns [].
Parameters
| Parameter | Type |
|---|---|
version | string | Version |
Returns
undefined | string[]
compare()
function compare(versionA, versionB): -1 | 0 | 1Defined in: index.ts:359
Same as sortMethod, but throws if either version is not valid. 1 if versionA is higher precedence than versionB -1 if versionA is lower precedence than versionB 0 if they have equal precedence
Parameters
Returns
-1 | 0 | 1
eq()
function eq(versionA, versionB): booleanDefined in: index.ts:414
true if versionA is equal to versionB. throws on invalid values
Parameters
Returns
boolean
filter()
function filter<T>(
list,
range,
includePrerelease): T[]Defined in: index.ts:226
Filter a list of versions to find all that match a given range.
Type Parameters
Parameters
| Parameter | Type | Default value |
|---|---|---|
list | T[] | undefined |
range | string | Range | undefined |
includePrerelease | boolean | false |
Returns
T[]
filterMethod()
function filterMethod(range, includePrerelease): (version) => booleanDefined in: index.ts:213
Method used by filter, for use in Array.filter directly.
Usage:
import { filterMethod } from '@vltpkg/semver'
const versions = ['1.2.3', '5.2.3', '2.3.4']
console.log(versions.filter(filterMethod('>=2.x')))
// ['5.2.3', '2.3.4']Parameters
| Parameter | Type | Default value |
|---|---|---|
range | string | Range | undefined |
includePrerelease | boolean | false |
Returns
Function
Parameters
| Parameter | Type |
|---|---|
version | string | Version |
Returns
boolean
gt()
function gt(versionA, versionB): booleanDefined in: index.ts:389
true if versionA is > versionB. throws on invalid values
Parameters
Returns
boolean
gte()
function gte(versionA, versionB): booleanDefined in: index.ts:394
true if versionA is >= versionB. throws on invalid values
Parameters
Returns
boolean
highest()
function highest(
list,
range,
includePrerelease): undefined | VersionDefined in: index.ts:237
Find the highest-precedence match for a range within a list of versions
Returns undefined if no match was found.
Parameters
Returns
undefined | Version
inc()
function inc(
version,
part,
prereleaseIdentifier?): VersionDefined in: index.ts:107
Increment the specified part of the version, and return the resulting object. If a Version object is provided, it will be modified in-place.
See Version.inc for full description.
Parameters
| Parameter | Type |
|---|---|
version | string | Version |
part | | "major" | "minor" | "patch" | "pre" | "premajor" | "preminor" | "prepatch" | "prerelease" |
prereleaseIdentifier? | string |
Returns
intersects()
function intersects(
r1,
r2,
includePrerelease?): booleanDefined in: index.ts:473
Return true if the range r1 intersects any of the ranges r2 r1 and r2 are either Range objects or range strings. Returns true if any version would satisfy both ranges.
Parameters
Returns
boolean
isRange()
function isRange(range): range is RangeDefined in: range.ts:6
Parameters
| Parameter | Type |
|---|---|
range | unknown |
Returns
range is Range
lowest()
function lowest(
list,
range,
includePrerelease): undefined | VersionDefined in: index.ts:307
Find the lowest-precedence match for a range within a list of versions
Returns undefined if no match was found.
Parameters
Returns
undefined | Version
lt()
function lt(versionA, versionB): booleanDefined in: index.ts:399
true if versionA is < versionB. throws on invalid values
Parameters
Returns
boolean
lte()
function lte(versionA, versionB): booleanDefined in: index.ts:404
true if versionA is <= versionB. throws on invalid values
Parameters
Returns
boolean
major()
function major(version): undefined | numberDefined in: index.ts:420
extract the major version number, or undefined if invalid
Parameters
| Parameter | Type |
|---|---|
version | string | Version |
Returns
undefined | number
minor()
function minor(version): undefined | numberDefined in: index.ts:423
extract the minor version number, or undefined if invalid
Parameters
| Parameter | Type |
|---|---|
version | string | Version |
Returns
undefined | number
neq()
function neq(versionA, versionB): booleanDefined in: index.ts:409
true if versionA is not equal to versionB. throws on invalid values
Parameters
Returns
boolean
parse()
function parse(version): undefined | VersionDefined in: index.ts:12
Return the parsed version string, or undefined if invalid
Parameters
| Parameter | Type |
|---|---|
version | string | Version |
Returns
undefined | Version
parseRange()
function parseRange(range, includePrerelease): undefined | RangeDefined in: index.ts:37
Return the parsed version range, or undefined if invalid
Parameters
| Parameter | Type | Default value |
|---|---|---|
range | string | Range | undefined |
includePrerelease | boolean | false |
Returns
undefined | Range
patch()
function patch(version): undefined | numberDefined in: index.ts:426
extract the patch version number, or undefined if invalid
Parameters
| Parameter | Type |
|---|---|
version | string | Version |
Returns
undefined | number
prerelease()
function prerelease(version): undefined | (string | number)[]Defined in: index.ts:432
extract the list of prerelease identifiers, or undefined if the version
is invalid. If no prerelease identifiers are present, returns [].
Parameters
| Parameter | Type |
|---|---|
version | string | Version |
Returns
undefined | (string | number)[]
rcompare()
function rcompare(versionA, versionB): -1 | 0 | 1Defined in: index.ts:383
Inverse of compare
Same as rsortMethod, but throws if either version is not valid.
-1 if versionA is higher precedence than versionB 1 if versionA is lower precedence than versionB 0 if they have equal precedence
Parameters
Returns
-1 | 0 | 1
rsort()
function rsort<T>(list): T[]Defined in: index.ts:170
Sort an array of version strings or objects in descending SemVer precedence order (ie, highest versions first).
Invalid version strings are sorted to the end of the array in ascending alphabetical order.
Note: when using this method, the list is cloned prior to sorting, to prevent surprising mutation. To sort the list in place, see rsortMethod.
Type Parameters
Parameters
| Parameter | Type |
|---|---|
list | T[] |
Returns
T[]
rsortedHighest()
function rsortedHighest(
list,
range,
includePrerelease): undefined | VersionDefined in: index.ts:287
Faster form of highest, for use when the list is sorted in reverse precedence order (higher-precedence versions first).
Note: This stops at the first match, and will produce incorrect results when the list is not properly sorted!
Parameters
Returns
undefined | Version
rsortedLowest()
function rsortedLowest(
list,
range,
includePrerelease): undefined | VersionDefined in: index.ts:346
Faster form of lowest, for use when the list is sorted in reverse precedence order (higher-precedence versions first).
Note: This stops at the first match, and will produce incorrect results when the list is not properly sorted!
Parameters
Returns
undefined | Version
rsortMethod()
function rsortMethod(a, b): numberDefined in: index.ts:187
The method used by rsort, exported for passing directly to
Array.sort.
Usage:
import { rsortMethod } from '@vltpkg/semver'
const versions = ['1.2.3', '5.2.3', '2.3.4']
console.log(versions.sort(rsortMethod))
// ['5.2.3', '2.3.4', '1.2.3']Parameters
Returns
number
satisfies()
function satisfies(
version,
range,
includePrerelease): booleanDefined in: index.ts:83
Return true if the version satisfies the range.
Parameters
Returns
boolean
sort()
function sort<T>(list): T[]Defined in: index.ts:155
Sort an array of version strings or objects in ascending SemVer precedence order (ie, lowest versions first).
Invalid version strings are sorted to the end of the array in ascending alphabetical order.
Note: when using this method, the list is cloned prior to sorting, to prevent surprising mutation. To sort the list in place, see sortMethod.
Type Parameters
Parameters
| Parameter | Type |
|---|---|
list | T[] |
Returns
T[]
sortedHighest()
function sortedHighest(
list,
range,
includePrerelease): undefined | VersionDefined in: index.ts:262
Faster form of highest, for use when the list is sorted in precedence order (lower-precedence versions first).
Note: This stops at the first match, and will produce incorrect results when the list is not properly sorted!
Parameters
Returns
undefined | Version
sortedLowest()
function sortedLowest(
list,
range,
includePrerelease): undefined | VersionDefined in: index.ts:332
Faster form of lowest, for use when the list is sorted in precedence order (lower-precedence versions first).
Note: This stops at the first match, and will produce incorrect results when the list is not properly sorted!
Parameters
Returns
undefined | Version
sortMethod()
function sortMethod(a, b): numberDefined in: index.ts:130
The method used by sort, exported for passing directly to
Array.sort.
Usage:
import { sortMethod } from '@vltpkg/semver'
const versions = ['1.2.3', '5.2.3', '2.3.4']
console.log(versions.sort(sortMethod))
// ['1.2.3', '2.3.4', '5.2.3']Parameters
Returns
number
stable()
function stable<T>(versions): T[]Defined in: index.ts:448
return all versions that do not have any prerelease identifiers
Type Parameters
Parameters
| Parameter | Type |
|---|---|
versions | T[] |
Returns
T[]
subset()
function subset(
r1,
r2,
includePrerelease?): booleanDefined in: index.ts:508
Check if range r1 is a subset of range r2. Returns true if every version that satisfies r1 also satisfies r2.
Parameters
Returns
boolean
valid()
function valid(version): booleanDefined in: index.ts:68
return true if the version is valid
Note: do not use this if you intend to immediately parse the version if it's
valid. Just use parse, and guard the possible undefined value, or
use Version.parse(..) to throw on invalid values.
Parameters
| Parameter | Type |
|---|---|
version | string | Version |
Returns
boolean
validRange()
function validRange(range): booleanDefined in: index.ts:77
return true if the range is valid
Note: do not use this if you intend to immediately parse the range if it's
valid. Just use parseRange, and guard the possible undefined value,
or use new Range(..) to throw on invalid values.
Parameters
| Parameter | Type |
|---|---|
range | string | Range |
Returns
boolean