@hyperfrontend/versioning/semversemver/
Semantic versioning utilities for parsing, comparing, formatting, and incrementing versions.
Overview
Complete implementation of the Semantic Versioning 2.0.0 specification. Parsing uses character-by-character state machines for predictable O(n) performance.
Usage Examples
Parsing Versions
import { parseVersion, parseVersionOrThrow, coerceVersion } from '@hyperfrontend/versioning'
// Safe parsing (returns null on invalid)
const v1 = parseVersion('1.2.3') // SemVer { major: 1, minor: 2, patch: 3 }
const v2 = parseVersion('invalid') // null
// Strict parsing (throws on invalid)
const v3 = parseVersionOrThrow('2.0.0-beta.1')
// Coerce with best effort
const v4 = coerceVersion('v1.2.3') // strips 'v' prefix
const v5 = coerceVersion('1.2') // normalizes to 1.2.0
Comparing Versions
import { compare, gt, lt, satisfies, parseVersion, parseRange } from '@hyperfrontend/versioning'
const a = parseVersion('1.0.0')!
const b = parseVersion('2.0.0')!
compare(a, b) // -1 (a < b)
gt(b, a) // true
lt(a, b) // true
// Range satisfaction
const range = parseRange('^1.0.0')!
satisfies(parseVersion('1.5.0')!, range) // true
satisfies(parseVersion('2.0.0')!, range) // false
Incrementing Versions
import { bump, parseVersion, format } from '@hyperfrontend/versioning'
const version = parseVersion('1.2.3')!
format(bump(version, 'major')) // '2.0.0'
format(bump(version, 'minor')) // '1.3.0'
format(bump(version, 'patch')) // '1.2.4'
format(bump(version, 'prerelease')) // '1.2.4-0'
Sorting Versions
import { sort, max, min, parseVersion, format } from '@hyperfrontend/versioning'
const versions = [parseVersion('2.0.0')!, parseVersion('1.0.0')!, parseVersion('1.5.0')!]
sort(versions).map(format) // ['1.0.0', '1.5.0', '2.0.0']
format(max(versions)!) // '2.0.0'
format(min(versions)!) // '1.0.0'
Design Principles
- Immutable — All operations return new objects
- Pure Functions — No side effects, deterministic output
- No Regex — Character-by-character parsing eliminates ReDoS
- Null Safety — Parse functions return
nullon invalid input - Factory Pattern — Use
create*functions for object construction
See Also
- changelog/ — Uses semver for version entries
- flow/ — Uses semver for bump calculations
- commits/ — Maps commit types to bump types
- registry/ — Queries published versions
- workspace/ — Cascade bump calculations
- Main README — Package overview and quick start
- ARCHITECTURE.md — Design principles and data flow
API Reference
ƒ Functions
Parameters
| Name | Type | Description |
|---|---|---|
§input | string | The string to coerce |
Returns
SemVerExample
Coerce partial versions to valid semver
coerceVersion('1') // { major: 1, minor: 0, patch: 0, ... }
coerceVersion('1.2') // { major: 1, minor: 2, patch: 0, ... }
coerceVersion('v2.0') // { major: 2, minor: 0, patch: 0, ... }Returns
-1 | 0 | 1Example
Compare two semantic versions
compare(parseVersion('1.0.0'), parseVersion('2.0.0')) // -1
compare(parseVersion('1.0.0'), parseVersion('1.0.0')) // 0
compare(parseVersion('2.0.0'), parseVersion('1.0.0')) // 1Returns
RangeExample
Create a range that matches any version
const anyRange = createAnyRange()
satisfies(parseVersionStrict('999.999.999'), anyRange) // => trueParameters
Returns
ComparatorExample
Create a comparator for version comparison
createComparator('>=', parseVersionStrict('1.0.0'))
// => { operator: '>=', version: { major: 1, minor: 0, patch: 0, ... } }Parameters
| Name | Type | Description |
|---|---|---|
§comparators | unknown | Array of comparators (AND logic) |
Returns
ComparatorSetExample
Create a comparator set with AND logic
createComparatorSet([gte100, lt200]) // AND: >=1.0.0 AND <2.0.0Parameters
| Name | Type | Description |
|---|---|---|
§version | SemVer | The exact version to match |
Returns
RangeExample
Create a range that matches exactly one version
const exact = createExactRange(parseVersionStrict('1.2.3'))
satisfies(parseVersionStrict('1.2.3'), exact) // => true
satisfies(parseVersionStrict('1.2.4'), exact) // => falseReturns
SemVerExample
Create first 1.0.0 release version
const first = createFirstRelease()
format(first) // => '1.0.0'Returns
SemVerExample
Create initial 0.0.0 version
const initial = createInitialVersion()
format(initial) // => '0.0.0'Parameters
Returns
RangeExample
Create a range with OR logic between sets
createRange([set1, set2], '>=1.0.0 || >=2.0.0 <3.0.0')Parameters
| Name | Type | Description |
|---|---|---|
§options | Partial<SemVer> & RequiredVersionComponents | Version components |
Returns
SemVerExample
Create a SemVer object from components
createSemVer({ major: 1, minor: 2, patch: 3 })
// => { major: 1, minor: 2, patch: 3, prerelease: [], build: [] }
createSemVer({ major: 1, minor: 0, patch: 0, prerelease: ['beta', '1'] })
// => 1.0.0-beta.1Returns
BumpTypeExample
Calculate the difference type between two versions
diff(parseVersion('1.0.0'), parseVersion('2.0.0')) // 'major'
diff(parseVersion('1.0.0'), parseVersion('1.1.0')) // 'minor'
diff(parseVersion('1.0.0'), parseVersion('1.0.1')) // 'patch'Returns
booleanExample
Check if two versions are equal
eq(parseVersionStrict('1.0.0'), parseVersionStrict('1.0.0')) // => true
eq(parseVersionStrict('1.0.0'), parseVersionStrict('1.0.1')) // => falseParameters
| Name | Type | Description |
|---|---|---|
§version | SemVer | The version to format |
Returns
stringExample
Format a version to its canonical string
format(parseVersionStrict('1.2.3')) // => '1.2.3'
format(createSemVer({ major: 1, minor: 0, patch: 0, prerelease: ['beta', '1'] }))
// => '1.0.0-beta.1'Parameters
| Name | Type | Description |
|---|---|---|
§comparator | Comparator | The comparator to format |
Returns
stringExample
Format a comparator to its string representation
formatComparator(createComparator('>=', parseVersionStrict('1.0.0')))
// => '>=1.0.0'Parameters
| Name | Type | Description |
|---|---|---|
§range | Range | The range to format |
Returns
stringExample
Format a range to its string representation
formatRange(parseRangeStrict('^1.0.0')) // => '^1.0.0'
formatRange(createAnyRange()) // => '*'Parameters
| Name | Type | Description |
|---|---|---|
§version | SemVer | The version to format |
Returns
stringExample
Format version without prerelease/build metadata
formatSimple(parseVersionStrict('1.2.3-beta.1+build')) // => '1.2.3'Returns
booleanExample
Check if version a is greater than b
gt(parseVersionStrict('2.0.0'), parseVersionStrict('1.0.0')) // => true
gt(parseVersionStrict('1.0.0'), parseVersionStrict('2.0.0')) // => falseReturns
booleanExample
Check if version a is greater than or equal to b
gte(parseVersionStrict('2.0.0'), parseVersionStrict('1.0.0')) // => true
gte(parseVersionStrict('1.0.0'), parseVersionStrict('1.0.0')) // => trueParameters
Returns
SemVerExample
Increment version by bump type
increment(parseVersion('1.2.3'), 'minor') // 1.3.0
increment(parseVersion('1.2.3'), 'major') // 2.0.0
increment(parseVersion('1.2.3'), 'prerelease', 'alpha') // 1.2.4-alpha.0Parameters
Returns
SemVerExample
Increment the prerelease portion of a version
incrementPrerelease(parseVersionStrict('1.0.0')) // => 1.0.1-alpha.0
incrementPrerelease(parseVersionStrict('1.0.0-alpha.0')) // => 1.0.0-alpha.1
incrementPrerelease(parseVersionStrict('1.0.0'), 'beta') // => 1.0.1-beta.0Parameters
| Name | Type | Description |
|---|---|---|
§version | SemVer | The version to check |
Returns
booleanExample
Check if version has prerelease identifiers
isPrerelease(parseVersionStrict('1.0.0-beta.1')) // => true
isPrerelease(parseVersionStrict('1.0.0')) // => falseParameters
| Name | Type | Description |
|---|---|---|
§version | SemVer | The version to check |
Returns
booleanExample
Check if version is a stable release
isStable(parseVersionStrict('1.0.0')) // => true
isStable(parseVersionStrict('0.9.0')) // => false (< 1.0.0)
isStable(parseVersionStrict('1.0.0-beta')) // => false (prerelease)Parameters
| Name | Type | Description |
|---|---|---|
§range | Range | The range to check |
Returns
booleanExample
Check if a range represents a wildcard
isWildcard(parseRangeStrict('*')) // => true
isWildcard(parseRangeStrict('^1.0.0')) // => falseReturns
booleanExample
Check if version a is less than b
lt(parseVersionStrict('1.0.0'), parseVersionStrict('2.0.0')) // => true
lt(parseVersionStrict('2.0.0'), parseVersionStrict('1.0.0')) // => falseReturns
booleanExample
Check if version a is less than or equal to b
lte(parseVersionStrict('1.0.0'), parseVersionStrict('1.0.0')) // => true
lte(parseVersionStrict('1.0.0'), parseVersionStrict('2.0.0')) // => trueParameters
| Name | Type | Description |
|---|---|---|
§versions | unknown | Array of versions |
Returns
SemVerExample
Get the maximum version from an array
const versions = [parseVersionStrict('1.0.0'), parseVersionStrict('2.0.0')]
max(versions) // => { major: 2, minor: 0, patch: 0, ... }
max([]) // => nullParameters
Returns
SemVerExample
Find the maximum version satisfying a range
const versions = ['1.0.0', '1.5.0', '2.0.0'].map(parseVersionStrict)
maxSatisfying(versions, parseRangeStrict('^1.0.0'))
// => { major: 1, minor: 5, patch: 0, ... }Parameters
| Name | Type | Description |
|---|---|---|
§versions | unknown | Array of versions |
Returns
SemVerExample
Get the minimum version from an array
const versions = [parseVersionStrict('1.0.0'), parseVersionStrict('2.0.0')]
min(versions) // => { major: 1, minor: 0, patch: 0, ... }
min([]) // => nullParameters
Returns
SemVerExample
Find the minimum version satisfying a range
const versions = ['1.0.0', '1.5.0', '2.0.0'].map(parseVersionStrict)
minSatisfying(versions, parseRangeStrict('^1.0.0'))
// => { major: 1, minor: 0, patch: 0, ... }Returns
booleanExample
Check if two versions are not equal
neq(parseVersionStrict('1.0.0'), parseVersionStrict('2.0.0')) // => true
neq(parseVersionStrict('1.0.0'), parseVersionStrict('1.0.0')) // => falseSupports:
- Exact: 1.2.3, =1.2.3
- Comparators: >1.0.0, >=1.0.0, <2.0.0, <=2.0.0
- Caret: ^1.2.3 (compatible with version)
- Tilde: ~1.2.3 (approximately equivalent)
- X-ranges: 1.x, 1.2.x, *
- Hyphen ranges: 1.0.0 - 2.0.0
- OR: 1.0.0 || 2.0.0
- AND: >=1.0.0 <2.0.0
Parameters
| Name | Type | Description |
|---|---|---|
§input | string | The range string to parse |
Returns
ParseRangeResultExample
Parse semver range strings
parseRange('^1.0.0') // => { success: true, range: ... }
parseRange('>=1.0.0 <2.0.0') // => { success: true, range: ... }
parseRange('1.0.0 || 2.0.0') // => { success: true, range: ... }Parameters
| Name | Type | Description |
|---|---|---|
§input | string | The range string to parse |
Returns
RangeExample
Parse range with strict validation
const range = parseRangeStrict('^1.0.0')
parseRangeStrict('invalid range!!!') // throws ErrorAccepts versions in the format: MAJOR.MINOR.PATCH[-prerelease][+build] Optional leading 'v' or '=' prefixes are stripped.
Parameters
| Name | Type | Description |
|---|---|---|
§input | string | The version string to parse |
Returns
ParseVersionResultExample
Parse a semantic version string
parseVersion('1.2.3') // { success: true, version: { major: 1, minor: 2, patch: 3, ... } }
parseVersion('v1.0.0-alpha.1+build.123') // { success: true, ... }
parseVersion('invalid') // { success: false, error: '...' }Parameters
| Name | Type | Description |
|---|---|---|
§input | string | The version string to parse |
Returns
SemVerExample
Parse version with strict validation
const v = parseVersionStrict('1.2.3')
// => { major: 1, minor: 2, patch: 3, prerelease: [], build: [] }
parseVersionStrict('invalid') // throws ErrorReturns
booleanExample
Check if version satisfies a range
satisfies(parseVersion('1.2.3'), parseRange('^1.0.0')) // true
satisfies(parseVersion('2.0.0'), parseRange('^1.0.0')) // falseParameters
Returns
booleanExample
Check if version satisfies a comparator
const gte100 = createComparator('>=', parseVersionStrict('1.0.0'))
satisfiesComparator(parseVersionStrict('2.0.0'), gte100) // => true
satisfiesComparator(parseVersionStrict('0.9.0'), gte100) // => falseParameters
| Name | Type | Description |
|---|---|---|
§versions | unknown | Array of versions to sort |
Returns
SemVer[]Example
Sort versions in ascending order
sort([v2, v1, v3]) // [v1, v2, v3]Parameters
| Name | Type | Description |
|---|---|---|
§versions | unknown | Array of versions to sort |
Returns
SemVer[]Example
Sort versions in descending order
sortDescending([v1, v3, v2]) // [v3, v2, v1]Parameters
| Name | Type | Description |
|---|---|---|
§version | SemVer | The version to strip |
Returns
SemVerExample
Strip build metadata from version
const v = parseVersionStrict('1.0.0+build.123')
format(stripBuild(v)) // => '1.0.0'Parameters
| Name | Type | Description |
|---|---|---|
§version | SemVer | The version to strip |
Returns
SemVerExample
Strip prerelease identifiers from version
const v = parseVersionStrict('1.0.0-beta.1')
format(stripPrerelease(v)) // => '1.0.0'◈ Interfaces
Examples:
- >=1.2.3 -> { operator: '>=', version: { major: 1, minor: 2, patch: 3 } }
- ^1.2.0 -> { operator: '^', version: { major: 1, minor: 2, patch: 0 } }
Example: ">=1.0.0 <2.0.0" -> two comparators in one set
Properties
Example: "^1.0.0 || ^2.0.0" -> two comparator sets
A version number in the format MAJOR.MINOR.PATCH with optional prerelease and build metadata.