Skip to content

Commit e7092b4

Browse files
committed
Improved coerce regex and added coerce to README.
- updated README with new CLI usage and a section for the coerce function - minimized how much was consumed by the coerce regex - added all coercion examples from the README to the coerce tests
1 parent 68cef2d commit e7092b4

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ semver.clean(' =v1.2.3 ') // '1.2.3'
2020
semver.satisfies('1.2.3', '1.x || >=2.5.0 || 5.0.0 - 7.2.3') // true
2121
semver.gt('1.2.3', '9.8.7') // false
2222
semver.lt('1.2.3', '9.8.7') // true
23+
semver.valid(semver.coerce('v2')) // '2.0.0'
24+
semver.valid(semver.coerce('42.6.7.9.3-alpha')) // '42.6.7'
2325
```
2426

2527
As a command-line utility:
@@ -52,6 +54,10 @@ Options:
5254
-l --loose
5355
Interpret versions and ranges loosely
5456

57+
-c --coerce
58+
Coerce a string into SemVer if possible
59+
(does not imply --loose)
60+
5561
Program exits successfully if any valid version satisfies
5662
all supplied ranges, and prints all satisfying versions.
5763

@@ -364,3 +370,15 @@ satisfy the range.
364370

365371
If you want to know if a version satisfies or does not satisfy a
366372
range, use the `satisfies(version, range)` function.
373+
374+
### Coercion
375+
376+
* `coerce(version)`: Coerces a string to semver if possible
377+
378+
This aims to provide a very forgiving translation of a non-semver
379+
string to semver. It looks for the first digit in a string, and
380+
consumes all remaining characters which satisfy at least a partial semver
381+
(e.g., `1`, `1.2`, `1.2.3`) up to the max permitted length (256 characters).
382+
Longer versions are simply truncated (`4.6.3.9.2-alpha2` becomes `4.6.3`).
383+
All surrounding text is simply ignored (`v3.4 replaces v3.3.1` becomes `3.4.0`).
384+
Only text which lacks digits will fail coercion (`version one` is not valid).

semver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@ function coerce(version) {
13061306
if (version.length > MAX_LENGTH)
13071307
return null;
13081308

1309-
var match = version.match(/([vV]?\d+(?:[.]\d+)*)/)
1309+
var match = version.match(/([vV]?\d+(?:[.]\d+){0,2})/)
13101310
if (match == null)
13111311
return null
13121312

test/coerce.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ test('\ncoerce tests', function(t) {
1414
function () { return '1.2.3'; },
1515
'',
1616
'.',
17+
'version one',
1718
tooLong
1819
].forEach(function (input) {
1920
var msg = 'coerce(' + input + ') should be null'
@@ -60,6 +61,10 @@ test('\ncoerce tests', function(t) {
6061
['version1', '1.0.0'],
6162
['version1.0', '1.0.0'],
6263
['version1.1', '1.1.0'],
64+
['42.6.7.9.3-alpha', '42.6.7'],
65+
['v2', '2.0.0'],
66+
['v3.4 replaces v3.3.1', '3.4.0'],
67+
['4.6.3.9.2-alpha2', '4.6.3'],
6368
[justRight, '12.1.1']
6469
].forEach(function (tuple) {
6570
var input = tuple[0]

0 commit comments

Comments
 (0)