]> git.cworth.org Git - obsolete/notmuch-web/blob - node_modules/express/lib/router/route.js
Install the "express" node module via npm
[obsolete/notmuch-web] / node_modules / express / lib / router / route.js
1
2 /*!
3  * Express - router - Route
4  * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
5  * MIT Licensed
6  */
7
8 /**
9  * Expose `Route`.
10  */
11
12 module.exports = Route;
13
14 /**
15  * Initialize `Route` with the given HTTP `method`, `path`,
16  * and callback `fn` and `options`.
17  *
18  * Options:
19  *
20  *   - `sensitive`   enable case-sensitive routes
21  *
22  * @param {String} method
23  * @param {String} path
24  * @param {Function} fn
25  * @param {Object} options.
26  * @api private
27  */
28
29 function Route(method, path, fn, options) {
30   options = options || {};
31   this.callback = fn;
32   this.path = path;
33   this.regexp = normalize(path, this.keys = [], options.sensitive);
34   this.method = method;
35 }
36
37 /**
38  * Normalize the given path string,
39  * returning a regular expression.
40  *
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"].
45  *
46  * @param  {String|RegExp} path
47  * @param  {Array} keys
48  * @param  {Boolean} sensitive
49  * @return {RegExp}
50  * @api private
51  */
52
53 function normalize(path, keys, sensitive) {
54   if (path instanceof RegExp) return path; 
55   path = path
56     .concat('/?')
57     .replace(/\/\(/g, '(?:/')
58     .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){
59       keys.push(key);
60       slash = slash || '';
61       return ''
62         + (optional ? '' : slash)
63         + '(?:'
64         + (optional ? slash : '')
65         + (format || '') + (capture || '([^/]+?)') + ')'
66         + (optional || '');
67     })
68     .replace(/([\/.])/g, '\\$1')
69     .replace(/\*/g, '(.+)');
70   return new RegExp('^' + path + '$', sensitive ? '' : 'i');
71 }