Skip to content

Commit 3fe447d

Browse files
Yanis Bensonsindresorhus
authored andcommitted
Improve the regex, require Node.js 8 (#15)
1 parent bea513f commit 3fe447d

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
language: node_js
22
node_js:
33
- '8'
4-
- '6'

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
'use strict';
2-
module.exports = () => /\bv?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\da-z-]+(?:\.[\da-z-]+)*)?(?:\+[\da-z-]+(?:\.[\da-z-]+)*)?\b/ig;
2+
module.exports = () => /(?<=^v?|\sv?)(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:[1-9]\d*|[\da-z-]*[a-z-][\da-z-]*)(?:\.(?:[1-9]\d*|[\da-z-]*[a-z-][\da-z-]*))*)?(?:\+[\da-z-]+(?:\.[\da-z-]+)*)?(?=$|\s)/ig;
3+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"url": "sindresorhus.com"
1111
},
1212
"engines": {
13-
"node": ">=6"
13+
"node": ">=8"
1414
},
1515
"scripts": {
1616
"test": "xo && ava"

test.js

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const fixture = [
1010
'2.7.2+asdf',
1111
'1.2.3-a.b.c.10.d.5',
1212
'2.7.2-foo+bar',
13-
'1.2.3-alpha.10.beta.0',
14-
'1.2.3-alpha.10.beta.0+build.unicorn.rainbow',
13+
'1.2.3-alpha.10.beta',
14+
'1.2.3-alpha.10.beta+build.unicorn.rainbow',
1515
'foo 0.0.0 bar 0.0.0'
1616
];
1717

@@ -31,7 +31,55 @@ test('returns semver on match', t => {
3131
t.deepEqual('foo 0.0.0 bar 0.1.1'.match(m()), ['0.0.0', '0.1.1']);
3232
});
3333

34-
// See #7
35-
test.failing('does not return tag prefix', t => {
34+
test('#7, does not return tag prefix', t => {
3635
t.deepEqual('v0.0.0'.match(m()), ['0.0.0']);
3736
});
37+
38+
test('#14, does not match substrings of longer semver-similar strings, respect semver2.0.0 clause 9', t => {
39+
const invalidStrings = [
40+
'1',
41+
'1.2',
42+
'1.2.3-0123',
43+
'1.2.3-0123.0123',
44+
'1.1.2+.123',
45+
'+invalid',
46+
'-invalid',
47+
'-invalid+invalid',
48+
'-invalid.01',
49+
'alpha',
50+
'alpha.beta',
51+
'alpha.beta.1',
52+
'alpha.1',
53+
'alpha+beta',
54+
'alpha_beta',
55+
'alpha.',
56+
'alpha..',
57+
'beta',
58+
'1.0.0-alpha_beta',
59+
'-alpha.',
60+
'1.0.0-alpha..',
61+
'1.0.0-alpha..1',
62+
'1.0.0-alpha...1',
63+
'1.0.0-alpha....1',
64+
'1.0.0-alpha.....1',
65+
'1.0.0-alpha......1',
66+
'1.0.0-alpha.......1',
67+
'01.1.1',
68+
'1.01.1',
69+
'1.1.01',
70+
'1.2',
71+
'1.2.3.DEV',
72+
'1.2-SNAPSHOT',
73+
'1.2.31.2.3----RC-SNAPSHOT.12.09.1--..12+788',
74+
'1.2-RC-SNAPSHOT',
75+
'-1.0.3-gamma+b7718',
76+
'+justmeta',
77+
'9.8.7+meta+meta',
78+
'9.8.7-whatever+meta+meta',
79+
'99999999999999999999999.999999999999999999.99999999999999999----RC-SNAPSHOT.12.09.1--------------------------------..12'
80+
];
81+
82+
for (const string of invalidStrings) {
83+
t.notRegex(string, m());
84+
}
85+
});

0 commit comments

Comments
 (0)