@hyperfrontend/versioning/semver/parseparse
Version and range parsing, with both strict and permissive variants.
parseVersion(input) accepts the common loose forms (leading v, missing patch, etc.) and returns a ParseVersionResult; parseVersionStrict rejects anything that isn't fully spec-compliant. coerceVersion is the most permissive option — it pulls a usable SemVer out of arbitrary inputs like "v1", "1.2", or "1.2.3.4" for use cases where semver-ish input must be normalized. parseRange and parseRangeStrict are the equivalents for npm-style range syntax (^1.2.0, ~1.2, >=1.0.0 <2.0.0, ...) and return a ParseRangeResult carrying either the parsed Range or a structured failure reason.
API Reference
ƒ Functions
Attempts to coerce a string into a valid semver. More lenient than parseVersion - accepts partial versions.
Parameters
| Name | Type | Description |
|---|---|---|
§input | string | The string to coerce |
Returns
SemVerThe parsed SemVer or null if coercion failed
Example
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, ... }Parses a semver range string.
Supports:
Supports:
- 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
ParseRangeResultA ParseRangeResult with the parsed range or error
Example
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: ... }Parses a range string, throwing on invalid input.
Parameters
| Name | Type | Description |
|---|---|---|
§input | string | The range string to parse |
Returns
RangeThe parsed Range
Example
Parse range with strict validation
const range = parseRangeStrict('^1.0.0')
parseRangeStrict('invalid range!!!') // throws ErrorParses a semantic version string.
Accepts versions in the format: MAJOR.MINOR.PATCH[-prerelease][+build] Optional leading 'v' or '=' prefixes are stripped.
Accepts 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
ParseVersionResultA ParseVersionResult with the parsed version or error
Example
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: '...' }Parses a version string, throwing on invalid input.
Parameters
| Name | Type | Description |
|---|---|---|
§input | string | The version string to parse |
Returns
SemVerThe parsed SemVer
Example
Parse version with strict validation
const v = parseVersionStrict('1.2.3')
// => { major: 1, minor: 2, patch: 3, prerelease: [], build: [] }
parseVersionStrict('invalid') // throws Error◈ Interfaces
Result of parsing a range string.