utils: added pathRegexp()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 54c3d5c · Browse files · 2 files·+48 added−40 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+48 added−40 removed
- other:+48 added−40 removed
Provenance
- Commit
0377540- Tree
a02fbf6- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
54c3d5c
2 files changed+48 added−40 removed
modified:lib/router/route.js+8 added-40 removed
12345678910111213141516171818 unmodified lines373839403441424344458 unmodified lines54555657515253545556575859606162636465666768697071727374757677787980818283848586878889
/*! * Express - router - Route * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca> * MIT Licensed */
/** * Module dependencies. */
var utils = require('../utils');
/** * Expose `Route`. */
module.exports = Route;18 unmodified lines options = options || {}; this.path = path; this.method = method; this.callbacks = callbacks; this.regexp = normalize(path this.regexp = utils.pathRegexp(path , this.keys = [] , options.sensitive , options.strict);}8 unmodified lines
Route.prototype.match = function(path){ return this.regexp.exec(path);};
/** * Normalize the given path string, * returning a regular expression. * * An empty array should be passed, * which will contain the placeholder * key names. For example "/user/:id" will * then contain ["id"]. * * @param {String|RegExp|Array} path * @param {Array} keys * @param {Boolean} sensitive * @param {Boolean} strict * @return {RegExp} * @api private */
function normalize(path, keys, sensitive, strict) { if (path instanceof RegExp) return path; if (path instanceof Array) path = '(' + path.join('|') + ')'; path = path .concat(strict ? '' : '/?') .replace(/\/\(/g, '(?:/') .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){ keys.push({ name: key, optional: !! optional }); slash = slash || ''; return '' + (optional ? '' : slash) + '(?:' + (optional ? slash : '') + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')' + (optional || ''); }) .replace(/([\/.])/g, '\\$1') .replace(/\*/g, '(.*)'); return new RegExp('^' + path + '$', sensitive ? '' : 'i');}modified:lib/utils.js+40 added
211 unmodified lines212 .replace(/"/g, '"')213 .replace(/</g, '<')214 .replace(/>/g, '>');215 };216+217+218+/**219+ * Normalize the given path string,220+ * returning a regular expression.221+ *222+ * An empty array should be passed,223+ * which will contain the placeholder224+ * key names. For example "/user/:id" will225+ * then contain ["id"].226+ *227+ * @param {String|RegExp|Array} path228+ * @param {Array} keys229+ * @param {Boolean} sensitive230+ * @param {Boolean} strict231+ * @return {RegExp}232+ * @api private233+ */234+235+exports.pathRegexp = function(path, keys, sensitive, strict) {236+ if (path instanceof RegExp) return path;237+ if (path instanceof Array) 238+ path = '(' + path.join('|') + ')';239+ path = path240+ .concat(strict ? '' : '/?')241+ .replace(/\/\(/g, '(?:/')242+ .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){243+ keys.push({ name: key, optional: !! optional });244+ slash = slash || '';245+ return ''246+ + (optional ? '' : slash)247+ + '(?:'248+ + (optional ? slash : '')249+ + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'250+ + (optional || '');251+ })252+ .replace(/([\/.])/g, '\\$1')253+ .replace(/\*/g, '(.*)');254+ return new RegExp('^' + path + '$', sensitive ? '' : 'i');255+}