Skip to content

Commit 626c9a7

Browse files
committed
Merge pull request #6 from blakeembrey/end-match
Match entire substring when using options.end
2 parents 88ecb9a + 39faf95 commit 626c9a7

File tree

5 files changed

+155
-7
lines changed

5 files changed

+155
-7
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
node_modules
2-
test/*.js
3-
test/*.css
2+
coverage

.npmignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,15 @@ function pathtoRegexp(path, keys, options) {
3333
path = path
3434
.concat(strict ? '' : '/?')
3535
.replace(/\/\(/g, '(?:/')
36-
.replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, function(_, slash, format, key, capture, optional, star){
36+
.replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function (_, slash, format, key, capture, optional) {
3737
keys.push({ name: key, optional: !! optional });
3838
slash = slash || '';
3939
return ''
4040
+ (optional ? '' : slash)
4141
+ '(?:'
4242
+ (optional ? slash : '')
43-
+ (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'
44-
+ (optional || '')
45-
+ (star ? '(/*)?' : '');
43+
+ (format || '') + (capture || (format ? '([^/.]+)' : '([^/]+)')) + ')'
44+
+ (optional || '');
4645
})
4746
.replace(/([\/.])/g, '\\$1')
4847
.replace(/\*/g, '(.*)');

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"name": "path-to-regexp",
33
"description": "Express style path to RegExp utility",
44
"version": "0.1.0",
5+
"scripts": {
6+
"test": "istanbul cover _mocha -- -R spec"
7+
},
58
"keywords": [
69
"express",
710
"regexp"
@@ -14,5 +17,9 @@
1417
"repository": {
1518
"type": "git",
1619
"url": "https://github.com/component/path-to-regexp.git"
20+
},
21+
"dependencies": {
22+
"mocha": "^1.17.1",
23+
"istanbul": "^0.2.6"
1724
}
1825
}

test.js

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
var pathToRegExp = require('./');
2+
var assert = require('assert');
3+
4+
describe('path-to-regexp', function () {
5+
describe('strings', function () {
6+
it('should match simple paths', function () {
7+
var m = pathToRegExp('/test').exec('/test');
8+
9+
assert.equal(m[0], '/test');
10+
});
11+
12+
it('should match express format params', function () {
13+
var params = [];
14+
var m = pathToRegExp('/:test', params).exec('/pathname');
15+
16+
assert.equal(m[1], 'pathname');
17+
assert.equal(params[0].name, 'test');
18+
assert.equal(params[0].optional, false);
19+
});
20+
21+
it('should do strict matches', function () {
22+
var params = [];
23+
var m = pathToRegExp('/:test', params, { strict: true }).exec('/route');
24+
25+
assert.equal(m[1], 'route');
26+
assert.equal(params[0].name, 'test');
27+
assert.equal(params[0].optional, false);
28+
});
29+
30+
it('should allow optional express format params', function () {
31+
var params = [];
32+
var re = pathToRegExp('/:test?', params);
33+
var m;
34+
35+
m = re.exec('/route');
36+
37+
assert.equal(m[1], 'route');
38+
assert.equal(params[0].name, 'test');
39+
assert.equal(params[0].optional, true);
40+
41+
m = re.exec('/');
42+
43+
assert.equal(m[1], undefined);
44+
assert.equal(params[0].name, 'test');
45+
assert.equal(params[0].optional, true);
46+
});
47+
48+
it('should allow express format param regexps', function () {
49+
var params = [];
50+
var m = pathToRegExp('/:page([0-9]+)', params).exec('/56');
51+
52+
assert.equal(m[1], '56');
53+
assert.equal(params[0].name, 'page');
54+
assert.equal(params[0].optional, false);
55+
});
56+
57+
it('should match without prefixed slash', function () {
58+
var params = [];
59+
var m = pathToRegExp(':test', params).exec('string');
60+
61+
assert.equal(m[0], 'string');
62+
assert.equal(m[1], 'string');
63+
assert.equal(params[0].name, 'test');
64+
assert.equal(params[0].optional, false);
65+
});
66+
67+
it('should not match format parts', function () {
68+
var params = [];
69+
var m = pathToRegExp('/:test.json', params).exec('/route.json');
70+
71+
assert.equal(m[1], 'route');
72+
assert.equal(params[0].name, 'test');
73+
assert.equal(params[0].optional, false);
74+
});
75+
76+
it('should match format parts', function () {
77+
var params = [];
78+
var m = pathToRegExp('/:test.:format', params).exec('/app.json');
79+
80+
assert.equal(m[1], 'app');
81+
assert.equal(m[2], 'json');
82+
assert.equal(params[0].name, 'test');
83+
assert.equal(params[0].optional, false);
84+
assert.equal(params[1].name, 'format');
85+
assert.equal(params[1].optional, false);
86+
});
87+
88+
it('should match optional trailing routes', function () {
89+
var params = [];
90+
var m = pathToRegExp('/test*', params).exec('/test/route');
91+
92+
assert.equal(m[1], '/route');
93+
assert.equal(params.length, 0);
94+
});
95+
96+
it('should match optional trailing routes after a param', function () {
97+
var params = [];
98+
var m = pathToRegExp('/:test*', params).exec('/test/route');
99+
100+
assert.equal(m[1], 'test');
101+
assert.equal(m[2], '/route');
102+
assert.equal(params[0].name, 'test');
103+
assert.equal(params[0].optional, false);
104+
assert.equal(m.length, 3);
105+
});
106+
107+
it('should do case insensitive matches', function () {
108+
var m = pathToRegExp('/test').exec('/TEST');
109+
110+
assert.equal(m[0], '/TEST');
111+
});
112+
113+
it('should do case sensitive matches', function () {
114+
var m = pathToRegExp('/test', [], { sensitive: true }).exec('/TEST');
115+
116+
assert.ok(!m);
117+
});
118+
119+
it('should do non-ending matches', function () {
120+
var params = [];
121+
var m = pathToRegExp('/:test', params, { end: false }).exec('/test/route');
122+
123+
assert.equal(m[1], 'test');
124+
assert.equal(params[0].name, 'test');
125+
assert.equal(params[0].optional, false);
126+
});
127+
});
128+
129+
describe('regexps', function () {
130+
it('should return the regexp', function () {
131+
assert.deepEqual(pathToRegExp(/.*/), /.*/);
132+
});
133+
});
134+
135+
describe('arrays', function () {
136+
it('should join arrays parts', function () {
137+
var re = pathToRegExp(['/test', '/route']);
138+
139+
assert.ok(re.exec('/test'));
140+
assert.ok(re.exec('/route'));
141+
assert.ok(!re.exec('/else'));
142+
});
143+
});
144+
});

0 commit comments

Comments
 (0)