2801f9146869f21e0e0b457958b7ae0f40867eec
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / express / node_modules / path-to-regexp / index.js
1 /**
2  * Expose `pathtoRegexp`.
3  */
4
5 module.exports = pathtoRegexp;
6
7 /**
8  * Normalize the given path string,
9  * returning a regular expression.
10  *
11  * An empty array should be passed,
12  * which will contain the placeholder
13  * key names. For example "/user/:id" will
14  * then contain ["id"].
15  *
16  * @param  {String|RegExp|Array} path
17  * @param  {Array} keys
18  * @param  {Object} options
19  * @return {RegExp}
20  * @api private
21  */
22
23 function pathtoRegexp(path, keys, options) {
24   options = options || {};
25   var strict = options.strict;
26   var end = options.end !== false;
27   var flags = options.sensitive ? '' : 'i';
28   keys = keys || [];
29
30   if (path instanceof RegExp) {
31     return path;
32   }
33
34   if (Array.isArray(path)) {
35     // Map array parts into regexps and return their source. We also pass
36     // the same keys and options instance into every generation to get
37     // consistent matching groups before we join the sources together.
38     path = path.map(function (value) {
39       return pathtoRegexp(value, keys, options).source;
40     });
41
42     return new RegExp('(?:' + path.join('|') + ')', flags);
43   }
44
45   path = ('^' + path + (strict ? '' : path[path.length - 1] === '/' ? '?' : '/?'))
46     .replace(/\/\(/g, '/(?:')
47     .replace(/([\/\.])/g, '\\$1')
48     .replace(/(\\\/)?(\\\.)?:(\w+)(\(.*?\))?(\*)?(\?)?/g, function (match, slash, format, key, capture, star, optional) {
49       slash = slash || '';
50       format = format || '';
51       capture = capture || '([^\\/' + format + ']+?)';
52       optional = optional || '';
53
54       keys.push({ name: key, optional: !!optional });
55
56       return ''
57         + (optional ? '' : slash)
58         + '(?:'
59         + format + (optional ? slash : '') + capture
60         + (star ? '((?:[\\/' + format + '].+?)?)' : '')
61         + ')'
62         + optional;
63     })
64     .replace(/\*/g, '(.*)');
65
66   // If the path is non-ending, match until the end or a slash.
67   path += (end ? '$' : (path[path.length - 1] === '/' ? '' : '(?=\\/|$)'));
68
69   return new RegExp(path, flags);
70 };