Skip to content
Myr

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

1 file+38 added−38 removed

modified:lib/express/core.js+38 added-38 removed
27 unmodified lines
28 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 lines
42 */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 */6869 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})8586// --- Router
118 unmodified lines
205function normalizePath(path) {206 return path.replace(/[\s\/]*$/g, '')207}208173/**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 */193194function 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}208209// --- Express210211Express = { 212 version: '0.0.1',
5 unmodified lines