Added Route#normalize()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 2e61731 · Browse files · 1 file·+38 added−38 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+38 added−38 removed
- other:+38 added−38 removed
Provenance
- Commit
2d75362- Tree
45305ea- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
2e61731
1 file changed+38 added−38 removed
modified:lib/express/core.js+38 added-38 removed
27 unmodified lines28 29 init: function(method, path, fn, options){30 this.method = method31 this.originalPath = path32- this.path = pathToRegexp(normalizePath(path))33- this.keys = Express.keys32+ this.path = this.normalize(normalizePath(path))33 this.fn = fn34 },35 36 /**5 unmodified lines42 */43 44 run: function(){45 return this.fn()46+ },47+ 48+ /**49+ * Normalize _path_. When a RegExp it is simply returned,50+ * otherwise a string is converted to a regular expression51+ * surrounded by ^$. So /user/:id/edit would become:52+ *53+ * \^/user/(.*?)\/edit\$/54+ *55+ * Each param key (:id) will be captured and placed in the56+ * params array, so param('id') would give the string captured.57+ *58+ * The following are valid routes:59+ *60+ * - /user/:id Ex: '/user/12'61+ * - /user/:id? Ex: '/user', '/user/12'62+ * - /report.:format Ex: '/report.pdf', 'report.csv'63+ *64+ * @param {string} path65+ * @return {regexp}66+ * @api private67+ */68+69+ normalize: function(path) {70+ var self = this71+ this.keys = []72+ if (path instanceof RegExp) return path73+ return new RegExp('^' + 74+ escapeRegexp(path75+ .replace(/\/:(\w+)\?/g, function(_, key){76+ self.keys.push(key)77+ return '(?:\/(.*?))?'78+ })79+ .replace(/:(\w+)/g, function(_, key){80+ self.keys.push(key)81+ return '(.*?)'82+ }), '/ [ ]') + '$', 'i')83 }84 })85 86 // --- Router118 unmodified lines205 function normalizePath(path) {206 return path.replace(/[\s\/]*$/g, '')207 }208 173-/**174- * Normalize _path_. When a RegExp it is simply returned,175- * otherwise a string is converted to a regular expression176- * surrounded by ^$. So /user/:id/edit would become:177- *178- * \^/user/(.*?)\/edit\$/179- *180- * Each param key (:id) will be captured and placed in the181- * params array, so param('id') would give the string captured.182- *183- * The following are valid routes:184- *185- * - /user/:id Ex: '/user/12'186- * - /user/:id? Ex: '/user', '/user/12'187- * - /report.:format Ex: '/report.pdf', 'report.csv'188- *189- * @param {string} path190- * @return {regexp}191- * @api private192- */193-194-function pathToRegexp(path) {195- Express.keys = []196- if (path instanceof RegExp) return path197- return new RegExp('^' + 198- escapeRegexp(path199- .replace(/\/:(\w+)\?/g, function(_, key){200- Express.keys.push(key)201- return '(?:\/(.*?))?'202- })203- .replace(/:(\w+)/g, function(_, key){204- Express.keys.push(key)205- return '(.*?)'206- }), '/ [ ]') + '$', 'i')207-}208-209 // --- Express210 211 Express = { 212 version: '0.0.1',5 unmodified lines