Coerce a Loose Version String into Valid SemVer
Convert loose version strings like v2.1, 3, or 1.2.3.4 into valid semver. Learn coercion rules and when automatic conversion is safe.
Detailed Explanation
Coercing Loose Version Strings
Real-world version strings are messy. Applications, APIs, and legacy systems often use formats that are not valid SemVer. Coercion is the process of extracting a valid semver from these loose strings.
Common Loose Formats
| Loose String | Coerced SemVer | Rule Applied |
|---|---|---|
v2.1.0 |
2.1.0 |
Strip "v" prefix |
2.1 |
2.1.0 |
Append missing PATCH as 0 |
3 |
3.0.0 |
Append missing MINOR and PATCH |
1.2.3.4 |
1.2.3 |
Drop extra components |
1.2.3 |
1.2.3 |
Trim whitespace |
=1.2.3 |
1.2.3 |
Strip leading operator |
v2.1.0-beta |
2.1.0-beta |
Strip prefix, keep pre-release |
When Coercion Is Safe
Coercion works well when:
- Displaying versions: Converting Git tags (
v1.2.3) for user display - Sorting mixed formats: Normalizing a list of versions from different sources
- Loose input fields: Accepting user input that is "close enough" to SemVer
When Coercion Is Dangerous
Coercion should be avoided when:
- Publishing packages: Always require strict SemVer for package registries
- Comparing for equality:
1.2coerced to1.2.0may not mean what the author intended - Four-part versions:
1.2.3.4might be a .NET assembly version where the 4th part matters
Coercion Algorithm
A typical coercion algorithm:
- Trim whitespace
- Strip leading
v,V, or= - Extract the first three dot-separated numeric groups
- Fill missing groups with
0 - Preserve pre-release and build metadata if present
The npm Approach
npm's semver.coerce() function scans the string for the first occurrence of a number and tries to build a version from it. For example, semver.coerce("version 4.2 is great") returns 4.2.0. This is intentionally aggressive to handle version strings embedded in prose or filenames.
Use Case
Building version parsers that accept input from heterogeneous sources like Git tags, Docker image tags, API headers, or user-provided strings that may not conform to strict SemVer.