3 * Express - router - Route
4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
12 module.exports = Route;
15 * Initialize `Route` with the given HTTP `method`, `path`,
16 * and callback `fn` and `options`.
20 * - `sensitive` enable case-sensitive routes
22 * @param {String} method
23 * @param {String} path
24 * @param {Function} fn
25 * @param {Object} options.
29 function Route(method, path, fn, options) {
30 options = options || {};
33 this.regexp = normalize(path, this.keys = [], options.sensitive);
38 * Normalize the given path string,
39 * returning a regular expression.
41 * An empty array should be passed,
42 * which will contain the placeholder
43 * key names. For example "/user/:id" will
44 * then contain ["id"].
46 * @param {String|RegExp} path
48 * @param {Boolean} sensitive
53 function normalize(path, keys, sensitive) {
54 if (path instanceof RegExp) return path;
57 .replace(/\/\(/g, '(?:/')
58 .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){
62 + (optional ? '' : slash)
64 + (optional ? slash : '')
65 + (format || '') + (capture || '([^/]+?)') + ')'
68 .replace(/([\/.])/g, '\\$1')
69 .replace(/\*/g, '(.+)');
70 return new RegExp('^' + path + '$', sensitive ? '' : 'i');