@sentrie/semver
The @sentrie/semver module provides semantic version comparison and validation utilities. Supports the “v” prefix (e.g., "v1.2.3" is equivalent to "1.2.3").
use { compare, isValid, satisfies, major, minor, patch } from @sentrie/semverFunctions
Section titled “Functions”compare(a: string, b: string): number
Section titled “compare(a: string, b: string): number”Compares two semantic version strings. Supports the “v” prefix - "v1.2.3" and "1.2.3" are treated as equivalent.
Parameters:
a- The first version string (e.g.,"1.2.3"or"v1.2.3")b- The second version string (e.g.,"1.2.4"or"v1.2.4")
Returns: -1 if a < b, 1 if a > b, 0 if a == b
Throws: Error if either version string is invalid
Example:
use { compare } from @sentrie/semverlet result = semver.compare("1.2.3", "1.2.4") // -1let result2 = semver.compare("v1.2.3", "1.2.3") // 0isValid(a: string): boolean
Section titled “isValid(a: string): boolean”Validates whether a string is a valid semantic version. Supports the “v” prefix - "v1.2.3" is considered valid.
Parameters:
a- The version string to validate (e.g.,"1.2.3","v1.2.3","1.0.0-alpha")
Returns: true if the string is a valid semantic version, false otherwise
Example:
use { isValid } from @sentrie/semverlet valid = semver.isValid("1.2.3") // truelet valid2 = semver.isValid("v1.2.3") // truelet invalid = semver.isValid("1.2") // falsestripPrefix(a: string): string
Section titled “stripPrefix(a: string): string”Strips the “v” or “V” prefix from a version string if present.
Parameters:
a- The version string (e.g.,"v1.2.3"or"V1.2.3")
Returns: The version string without the prefix (e.g., "1.2.3")
Example:
use { stripPrefix } from @sentrie/semverlet stripped = semver.stripPrefix("v1.2.3") // "1.2.3"let noChange = semver.stripPrefix("1.2.3") // "1.2.3"satisfies(version: string, constraint: string): boolean
Section titled “satisfies(version: string, constraint: string): boolean”Checks if a version satisfies a constraint. Supports constraint ranges like ">=1.0.0 <2.0.0", "^1.2.0", "~1.2.0", etc.
Parameters:
version- The version string to check (e.g.,"1.2.3"or"v1.2.3")constraint- The constraint string (e.g.,">=1.0.0 <2.0.0","^1.2.0")
Returns: true if the version satisfies the constraint, false otherwise
Throws: Error if either version or constraint string is invalid
Example:
use { satisfies } from @sentrie/semverlet satisfies1 = semver.satisfies("1.2.3", ">=1.0.0 <2.0.0") // truelet satisfies2 = semver.satisfies("2.0.0", "^1.2.0") // falsemajor(version: string): number
Section titled “major(version: string): number”Gets the major version number from a version string.
Parameters:
version- The version string (e.g.,"1.2.3"or"v1.2.3")
Returns: The major version number (e.g., 1 for "1.2.3")
Throws: Error if the version string is invalid
Example:
use { major } from @sentrie/semverlet maj = semver.major("1.2.3") // 1let maj2 = semver.major("2.0.0-alpha") // 2minor(version: string): number
Section titled “minor(version: string): number”Gets the minor version number from a version string.
Parameters:
version- The version string (e.g.,"1.2.3"or"v1.2.3")
Returns: The minor version number (e.g., 2 for "1.2.3")
Throws: Error if the version string is invalid
Example:
use { minor } from @sentrie/semverlet min = semver.minor("1.2.3") // 2let min2 = semver.minor("1.0.0") // 0patch(version: string): number
Section titled “patch(version: string): number”Gets the patch version number from a version string.
Parameters:
version- The version string (e.g.,"1.2.3"or"v1.2.3")
Returns: The patch version number (e.g., 3 for "1.2.3")
Throws: Error if the version string is invalid
Example:
use { patch } from @sentrie/semverlet pat = semver.patch("1.2.3") // 3let pat2 = semver.patch("1.2.0") // 0prerelease(version: string): string | null
Section titled “prerelease(version: string): string | null”Gets the prerelease identifier from a version string.
Parameters:
version- The version string (e.g.,"1.2.3-alpha.1"or"1.0.0-beta")
Returns: The prerelease identifier (e.g., "alpha.1") or null if not present
Throws: Error if the version string is invalid
Example:
use { prerelease } from @sentrie/semverlet pre = semver.prerelease("1.2.3-alpha.1") // "alpha.1"let noPre = semver.prerelease("1.2.3") // nullmetadata(version: string): string | null
Section titled “metadata(version: string): string | null”Gets the build metadata from a version string.
Parameters:
version- The version string (e.g.,"1.2.3+001"or"1.0.0+exp.sha.5114f85")
Returns: The build metadata (e.g., "001") or null if not present
Throws: Error if the version string is invalid
Example:
use { metadata } from @sentrie/semverlet meta = semver.metadata("1.2.3+001") // "001"let noMeta = semver.metadata("1.2.3") // nullComplete Example
Section titled “Complete Example”namespace com/example/version
policy mypolicy { use { compare, satisfies, major, minor, patch } from @sentrie/semver fact currentVersion!: string fact requiredVersion!: string fact versionConstraint!: string
rule checkVersion = default false { let comparison = semver.compare(currentVersion, requiredVersion) let satisfiesConstraint = semver.satisfies(currentVersion, versionConstraint) let majorVersion = semver.major(currentVersion) yield comparison >= 0 and satisfiesConstraint and majorVersion >= 1 }
export decision of checkVersion}Constraint Syntax
Section titled “Constraint Syntax”The satisfies function supports various constraint formats:
- Range:
">=1.0.0 <2.0.0"- Version must be >= 1.0.0 and < 2.0.0 - Caret:
"^1.2.0"- Compatible with version 1.2.0 (>= 1.2.0 < 2.0.0) - Tilde:
"~1.2.0"- Approximately equivalent to 1.2.0 (>= 1.2.0 < 1.3.0) - Exact:
"1.2.3"- Exactly version 1.2.3 - Greater/Less:
">=1.0.0","<2.0.0"- Single-sided constraints
Version Format
Section titled “Version Format”Semantic versions follow the format: MAJOR.MINOR.PATCH[-PRERELEASE][+BUILD]
- MAJOR - Incremented for incompatible API changes
- MINOR - Incremented for backwards-compatible functionality additions
- PATCH - Incremented for backwards-compatible bug fixes
- PRERELEASE - Optional prerelease identifier (e.g.,
alpha,beta,rc.1) - BUILD - Optional build metadata (e.g.,
+001,+exp.sha.5114f85)