Skip to content

Commit 3321055

Browse files
committed
use path-to-regexp
1 parent 5572897 commit 3321055

File tree

3 files changed

+5
-47
lines changed

3 files changed

+5
-47
lines changed

lib/router/layer.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
var utils = require('../utils')
2-
, debug = require('debug')('express:router:layer')
1+
var utils = require('../utils');
2+
var pathRegexp = require('path-to-regexp');
3+
var debug = require('debug')('express:router:layer');
34

45
function Layer(path, options, fn) {
56
if (!(this instanceof Layer)) {
@@ -10,11 +11,7 @@ function Layer(path, options, fn) {
1011
options = options || {};
1112
this.path = path;
1213
this.params = {};
13-
this.regexp = utils.pathRegexp(path
14-
, this.keys = []
15-
, options.sensitive
16-
, options.strict
17-
, options.end);
14+
this.regexp = pathRegexp(path, this.keys = [], options);
1815
this.handle = fn;
1916
}
2017

lib/utils.js

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -119,46 +119,6 @@ function acceptParams(str, index) {
119119
return ret;
120120
}
121121

122-
/**
123-
* Normalize the given path string,
124-
* returning a regular expression.
125-
*
126-
* An empty array should be passed,
127-
* which will contain the placeholder
128-
* key names. For example "/user/:id" will
129-
* then contain ["id"].
130-
*
131-
* @param {String|RegExp|Array} path
132-
* @param {Array} keys
133-
* @param {Boolean} sensitive
134-
* @param {Boolean} strict
135-
* @param {Boolean} end (whether to append $ to regex)
136-
* @return {RegExp}
137-
* @api private
138-
*/
139-
140-
exports.pathRegexp = function(path, keys, sensitive, strict, end) {
141-
if (toString.call(path) == '[object RegExp]') return path;
142-
if (Array.isArray(path)) path = '(' + path.join('|') + ')';
143-
path = path
144-
.concat(strict ? '' : '/?')
145-
.replace(/\/\(/g, '(?:/')
146-
.replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, function(_, slash, format, key, capture, optional, star){
147-
keys.push({ name: key, optional: !! optional });
148-
slash = slash || '';
149-
return ''
150-
+ (optional ? '' : slash)
151-
+ '(?:'
152-
+ (optional ? slash : '')
153-
+ (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'
154-
+ (optional || '')
155-
+ (star ? '(/*)?' : '');
156-
})
157-
.replace(/([\/.])/g, '\\$1')
158-
.replace(/\*/g, '(.*)');
159-
return new RegExp('^' + path + ((end) ? '$' : ''), sensitive ? '' : 'i');
160-
}
161-
162122
/**
163123
* Parse the `req` url with memoization.
164124
*

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"escape-html": "1.0.1",
3737
"qs": "0.6.6",
3838
"serve-static": "1.0.1",
39+
"path-to-regexp": "0.1.0",
3940
"debug": ">= 0.7.3 < 1"
4041
},
4142
"devDependencies": {

0 commit comments

Comments
 (0)