path-to-regexp
Advanced tools
Comparing version 0.1.2 to 0.1.3
{ | ||
"name": "path-to-regexp", | ||
"description": "Express style path to RegExp utility", | ||
"version": "0.1.0", | ||
"version": "0.1.3", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "express", |
@@ -0,2 +1,7 @@ | ||
0.1.3 / 2014-07-06 | ||
================== | ||
* Better array support | ||
* Improved support for trailing slash in non-ending mode | ||
0.1.0 / 2014-03-06 | ||
@@ -3,0 +8,0 @@ ================== |
28
index.js
@@ -25,12 +25,23 @@ /** | ||
options = options || {}; | ||
var sensitive = options.sensitive; | ||
var strict = options.strict; | ||
var end = options.end !== false; | ||
var flags = options.sensitive ? '' : 'i'; | ||
keys = keys || []; | ||
if (path instanceof RegExp) return path; | ||
if (path instanceof Array) path = '(' + path.join('|') + ')'; | ||
if (path instanceof RegExp) { | ||
return path; | ||
} | ||
path = path | ||
.concat(strict ? '' : '/?') | ||
if (Array.isArray(path)) { | ||
// Map array parts into regexps and return their source. We also pass | ||
// the same keys and options instance into every generation to get | ||
// consistent matching groups before we join the sources together. | ||
path = path.map(function (value) { | ||
return pathtoRegexp(value, keys, options).source; | ||
}); | ||
return new RegExp('(?:' + path.join('|') + ')', flags); | ||
} | ||
path = ('^' + path + (strict ? '' : path[path.length - 1] === '/' ? '?' : '/?')) | ||
.replace(/\/\(/g, '/(?:') | ||
@@ -41,3 +52,3 @@ .replace(/([\/\.])/g, '\\$1') | ||
format = format || ''; | ||
capture = capture || '([^/' + format + ']+?)'; | ||
capture = capture || '([^\\/' + format + ']+?)'; | ||
optional = optional || ''; | ||
@@ -57,3 +68,6 @@ | ||
return new RegExp('^' + path + (end ? '$' : '(?=\/|$)'), sensitive ? '' : 'i'); | ||
// If the path is non-ending, match until the end or a slash. | ||
path += (end ? '$' : (path[path.length - 1] === '/' ? '' : '(?=\\/|$)')); | ||
return new RegExp(path, flags); | ||
}; |
{ | ||
"name": "path-to-regexp", | ||
"description": "Express style path to RegExp utility", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"scripts": { | ||
@@ -6,0 +6,0 @@ "test": "istanbul cover _mocha -- -R spec" |
128
test.js
@@ -49,2 +49,26 @@ var pathToRegExp = require('./'); | ||
it('should do strict matches with trailing slashes', function () { | ||
var params = []; | ||
var re = pathToRegExp('/:test/', params, { strict: true }); | ||
var m; | ||
assert.equal(params.length, 1); | ||
assert.equal(params[0].name, 'test'); | ||
assert.equal(params[0].optional, false); | ||
m = re.exec('/route'); | ||
assert.ok(!m); | ||
m = re.exec('/route/'); | ||
assert.equal(m.length, 2); | ||
assert.equal(m[0], '/route/'); | ||
assert.equal(m[1], 'route'); | ||
m = re.exec('/route//'); | ||
assert.ok(!m); | ||
}); | ||
it('should allow optional express format params', function () { | ||
@@ -392,15 +416,71 @@ var params = []; | ||
it('should match trailing slashes in non-ending non-strict mode', function () { | ||
var params = []; | ||
var re = pathToRegExp('/route/', params, { end: false }); | ||
var m; | ||
assert.equal(params.length, 0); | ||
m = re.exec('/route/'); | ||
assert.equal(m.length, 1); | ||
assert.equal(m[0], '/route/'); | ||
m = re.exec('/route/test'); | ||
assert.equal(m.length, 1); | ||
assert.equal(m[0], '/route'); | ||
m = re.exec('/route'); | ||
assert.equal(m.length, 1); | ||
assert.equal(m[0], '/route'); | ||
m = re.exec('/route//'); | ||
assert.equal(m.length, 1); | ||
assert.equal(m[0], '/route/'); | ||
}); | ||
it('should match trailing slashing in non-ending strict mode', function () { | ||
var params = []; | ||
var re = pathToRegExp('/route/', params, { end: false, strict: true }); | ||
assert.equal(params.length, 0); | ||
m = re.exec('/route/'); | ||
assert.equal(m.length, 1); | ||
assert.equal(m[0], '/route/'); | ||
m = re.exec('/route/test'); | ||
assert.equal(m.length, 1); | ||
assert.equal(m[0], '/route/'); | ||
m = re.exec('/route'); | ||
assert.ok(!m); | ||
m = re.exec('/route//'); | ||
assert.equal(m.length, 1); | ||
assert.equal(m[0], '/route/'); | ||
}); | ||
it('should not match trailing slashes in non-ending strict mode', function () { | ||
var params = []; | ||
var re = pathToRegExp('/:test', params, { end: false, strict: true }); | ||
var re = pathToRegExp('/route', params, { end: false, strict: true }); | ||
assert.equal(params.length, 1); | ||
assert.equal(params[0].name, 'test'); | ||
assert.equal(params[0].optional, false); | ||
assert.equal(params.length, 0); | ||
m = re.exec('/test/'); | ||
m = re.exec('/route'); | ||
assert.equal(m.length, 2); | ||
assert.equal(m[0], '/test'); | ||
assert.equal(m[1], 'test'); | ||
assert.equal(m.length, 1); | ||
assert.equal(m[0], '/route'); | ||
m = re.exec('/route/'); | ||
assert.ok(m.length, 1); | ||
assert.equal(m[0], '/route'); | ||
}); | ||
@@ -507,7 +587,33 @@ | ||
assert.ok(re.exec('/test')); | ||
assert.ok(re.exec('/route')); | ||
assert.ok(!re.exec('/else')); | ||
assert.ok(re.test('/test')); | ||
assert.ok(re.test('/route')); | ||
assert.ok(!re.test('/else')); | ||
}); | ||
it('should match parts properly', function () { | ||
var params = []; | ||
var re = pathToRegExp(['/:test', '/test/:route'], params); | ||
var m; | ||
assert.equal(params.length, 2); | ||
assert.equal(params[0].name, 'test'); | ||
assert.equal(params[0].optional, false); | ||
assert.equal(params[1].name, 'route'); | ||
assert.equal(params[1].optional, false); | ||
m = re.exec('/route'); | ||
assert.equal(m.length, 3); | ||
assert.equal(m[0], '/route'); | ||
assert.equal(m[1], 'route'); | ||
assert.equal(m[2], undefined); | ||
m = re.exec('/test/path'); | ||
assert.equal(m.length, 3); | ||
assert.equal(m[0], '/test/path'); | ||
assert.equal(m[1], undefined); | ||
assert.equal(m[2], 'path'); | ||
}); | ||
}); | ||
}); |
20344
519