@hyperfrontend/versioning/semver/format

format

String formatting for semver values, ranges, and comparators.

format(version) produces the canonical MAJOR.MINOR.PATCH[-prerelease][+build] rendering for a SemVer. formatSimple strips prerelease/build metadata when only the numeric core is wanted. formatRange and formatComparator round-trip a parsed Range or Comparator back to the npm-flavored range syntax they came from.

API Reference

ƒ Functions

§function

format(version: SemVer): string

Converts a SemVer to its canonical string representation.

Parameters

NameTypeDescription
§version
SemVer
The version to format

Returns

string
The version string (e.g., "1.2.3-alpha.1+build.123")

Example

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'
§function

formatComparator(comparator: Comparator): string

Converts a Comparator to its string representation.

Parameters

NameTypeDescription
§comparator
Comparator
The comparator to format

Returns

string
The comparator string (e.g., ">=1.0.0")

Example

Format a comparator to its string representation

formatComparator(createComparator('>=', parseVersionStrict('1.0.0')))
// => '>=1.0.0'
§function

formatRange(range: Range): string

Converts a Range to its string representation.

Parameters

NameTypeDescription
§range
Range
The range to format

Returns

string
The range string

Example

Format a range to its string representation

formatRange(parseRangeStrict('^1.0.0')) // => '^1.0.0'
formatRange(createAnyRange()) // => '*'
§function

formatSimple(version: SemVer): string

Converts a SemVer to a string without prerelease/build.

Parameters

NameTypeDescription
§version
SemVer
The version to format

Returns

string
The version string (e.g., "1.2.3")

Example

Format version without prerelease/build metadata

formatSimple(parseVersionStrict('1.2.3-beta.1+build')) // => '1.2.3'