path-to-regexp
Advanced tools
Comparing version 0.2.1 to 0.2.2
{ | ||
"name": "path-to-regexp", | ||
"description": "Express style path to RegExp utility", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "express", |
@@ -0,1 +1,7 @@ | ||
0.2.2 / 2014-07-06 | ||
================== | ||
* A passed in trailing slash in non-strict mode will become optional | ||
* In non-end mode, the optional trailing slash will only match at the end | ||
0.2.1 / 2014-06-11 | ||
@@ -2,0 +8,0 @@ ================== |
49
index.js
@@ -93,7 +93,10 @@ /** | ||
var repeat = suffix === '+' || suffix === '*'; | ||
var optional = suffix === '?' || suffix === '*'; | ||
keys.push({ | ||
name: key || index++, | ||
delimiter: prefix || '/', | ||
optional: suffix === '?' || suffix === '*', | ||
repeat: suffix === '+' || suffix === '*' | ||
optional: optional, | ||
repeat: repeat | ||
}); | ||
@@ -109,12 +112,9 @@ | ||
// More complex regexp is required for suffix support. | ||
if (suffix) { | ||
if (suffix === '+') { | ||
return prefix + '(' + capture + '(?:' + prefix + capture + ')*)' | ||
} | ||
// Allow parameters to be repeated more than once. | ||
if (repeat) { | ||
capture = capture + '(?:' + prefix + capture + ')*'; | ||
} | ||
if (suffix === '*') { | ||
return '(?:' + prefix + '(' + capture + '(?:' + prefix + capture + ')*|' + capture + '))?'; | ||
} | ||
// Allow a parameter to be optional. | ||
if (optional) { | ||
return '(?:' + prefix + '(' + capture + '))?'; | ||
@@ -127,18 +127,21 @@ } | ||
if (path[path.length - 1] !== '/') { | ||
// If we are doing a non-ending match, we need to prompt the matching groups | ||
// to match as much as possible. To do this, we add a positive lookahead for | ||
// the next path fragment or the end. However, if the regexp already ends | ||
// in a path fragment, we'll run into problems. | ||
if (!end) { | ||
path += '(?=\\/|$)'; | ||
} | ||
// Check whether the path ends in a slash as it alters some match behaviour. | ||
var endsWithSlash = path[path.length - 1] === '/'; | ||
// Allow trailing slashes to be matched in non-strict, ending mode. | ||
if (end && !strict) { | ||
path += '\\/?'; | ||
} | ||
// In non-strict mode we allow an optional trailing slash in the match. If | ||
// the path to match already ended with a slash, we need to remove it for | ||
// consistency. The slash is only valid at the very end of a path match, not | ||
// anywhere in the middle. This is important for non-ending mode, otherwise | ||
// "/test/" will match "/test//route". | ||
if (!strict) { | ||
path = (endsWithSlash ? path.slice(0, -2) : path) + '(?:\\/(?=$))?'; | ||
} | ||
// In non-ending mode, we need prompt the capturing groups to match as much | ||
// as possible by using a positive lookahead for the end or next path segment. | ||
if (!end) { | ||
path += strict && endsWithSlash ? '' : '(?=\\/|$)'; | ||
} | ||
return new RegExp('^' + path + (end ? '$' : ''), flags); | ||
}; |
{ | ||
"name": "path-to-regexp", | ||
"description": "Express style path to RegExp utility", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"scripts": { | ||
@@ -6,0 +6,0 @@ "test": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec" |
49
test.js
@@ -35,2 +35,3 @@ var util = require('util'); | ||
['/test', [], '/test/', ['/test/']], | ||
['/test/', [], '/test', ['/test']], | ||
['/test/', [], '/test/', ['/test/']], | ||
@@ -59,3 +60,21 @@ ['/test/', [], '/test//', null], | ||
['/test', [], '/test', ['/test'], { end: false }], | ||
['/test', [], '/test/', ['/test/'], { end: false }], | ||
['/test', [], '/test/route', ['/test'], { end: false }], | ||
['/test/', [], '/test/route', ['/test'], { end: false }], | ||
['/test/', [], '/test//', ['/test'], { end: false }], | ||
['/test/', [], '/test//route', ['/test'], { end: false }], | ||
[ | ||
'/:test', | ||
[{ name: 'test', delimiter: '/', optional: false, repeat: false }], | ||
'/route', | ||
['/route', 'route'], | ||
{ end: false } | ||
], | ||
[ | ||
'/:test/', | ||
[{ name: 'test', delimiter: '/', optional: false, repeat: false }], | ||
'/route', | ||
['/route', 'route'], | ||
{ end: false } | ||
], | ||
@@ -74,2 +93,30 @@ /** | ||
['/test.json', [], '/test.json.hbs', null, { end: false, strict: true }], | ||
[ | ||
'/:test', | ||
[{ name: 'test', delimiter: '/', optional: false, repeat: false }], | ||
'/route', | ||
['/route', 'route'], | ||
{ end: false, strict: true } | ||
], | ||
[ | ||
'/:test', | ||
[{ name: 'test', delimiter: '/', optional: false, repeat: false }], | ||
'/route/', | ||
['/route', 'route'], | ||
{ end: false, strict: true } | ||
], | ||
[ | ||
'/:test/', | ||
[{ name: 'test', delimiter: '/', optional: false, repeat: false }], | ||
'/route/', | ||
['/route/', 'route'], | ||
{ end: false, strict: true } | ||
], | ||
[ | ||
'/:test/', | ||
[{ name: 'test', delimiter: '/', optional: false, repeat: false }], | ||
'/route', | ||
null, | ||
{ end: false, strict: true } | ||
], | ||
@@ -404,3 +451,3 @@ /** | ||
'route/', | ||
['route', 'route'], | ||
['route/', 'route'], | ||
{ end: false } | ||
@@ -407,0 +454,0 @@ ], |
30981
973