| 1 | /**
|
|---|
| 2 | * Expose `pathToRegexp`.
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | module.exports = pathToRegexp;
|
|---|
| 6 |
|
|---|
| 7 | /**
|
|---|
| 8 | * Match matching groups in a regular expression.
|
|---|
| 9 | */
|
|---|
| 10 | var MATCHING_GROUP_REGEXP = /\\.|\((?:\?<(.*?)>)?(?!\?)/g;
|
|---|
| 11 |
|
|---|
| 12 | /**
|
|---|
| 13 | * Normalize the given path string,
|
|---|
| 14 | * returning a regular expression.
|
|---|
| 15 | *
|
|---|
| 16 | * An empty array should be passed,
|
|---|
| 17 | * which will contain the placeholder
|
|---|
| 18 | * key names. For example "/user/:id" will
|
|---|
| 19 | * then contain ["id"].
|
|---|
| 20 | *
|
|---|
| 21 | * @param {String|RegExp|Array} path
|
|---|
| 22 | * @param {Array} keys
|
|---|
| 23 | * @param {Object} options
|
|---|
| 24 | * @return {RegExp}
|
|---|
| 25 | * @api private
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | function pathToRegexp(path, keys, options) {
|
|---|
| 29 | options = options || {};
|
|---|
| 30 | keys = keys || [];
|
|---|
| 31 | var strict = options.strict;
|
|---|
| 32 | var end = options.end !== false;
|
|---|
| 33 | var flags = options.sensitive ? '' : 'i';
|
|---|
| 34 | var lookahead = options.lookahead !== false;
|
|---|
| 35 | var extraOffset = 0;
|
|---|
| 36 | var keysOffset = keys.length;
|
|---|
| 37 | var i = 0;
|
|---|
| 38 | var name = 0;
|
|---|
| 39 | var pos = 0;
|
|---|
| 40 | var backtrack = '';
|
|---|
| 41 | var m;
|
|---|
| 42 |
|
|---|
| 43 | if (path instanceof RegExp) {
|
|---|
| 44 | while (m = MATCHING_GROUP_REGEXP.exec(path.source)) {
|
|---|
| 45 | if (m[0][0] === '\\') continue;
|
|---|
| 46 |
|
|---|
| 47 | keys.push({
|
|---|
| 48 | name: m[1] || name++,
|
|---|
| 49 | optional: false,
|
|---|
| 50 | offset: m.index
|
|---|
| 51 | });
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | return path;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | if (Array.isArray(path)) {
|
|---|
| 58 | // Map array parts into regexps and return their source. We also pass
|
|---|
| 59 | // the same keys and options instance into every generation to get
|
|---|
| 60 | // consistent matching groups before we join the sources together.
|
|---|
| 61 | path = path.map(function (value) {
|
|---|
| 62 | return pathToRegexp(value, keys, options).source;
|
|---|
| 63 | });
|
|---|
| 64 |
|
|---|
| 65 | return new RegExp(path.join('|'), flags);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | if (typeof path !== 'string') {
|
|---|
| 69 | throw new TypeError('path must be a string, array of strings, or regular expression');
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | path = path.replace(
|
|---|
| 73 | /\\.|(\/)?(\.)?:(\w+)(\(.*?\))?(\*)?(\?)?|[.*]|\/\(/g,
|
|---|
| 74 | function (match, slash, format, key, capture, star, optional, offset) {
|
|---|
| 75 | if (match[0] === '\\') {
|
|---|
| 76 | backtrack += match;
|
|---|
| 77 | pos += 2;
|
|---|
| 78 | return match;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | if (match === '.') {
|
|---|
| 82 | backtrack += '\\.';
|
|---|
| 83 | extraOffset += 1;
|
|---|
| 84 | pos += 1;
|
|---|
| 85 | return '\\.';
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | if (slash || format) {
|
|---|
| 89 | backtrack = '';
|
|---|
| 90 | } else {
|
|---|
| 91 | backtrack += path.slice(pos, offset);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | pos = offset + match.length;
|
|---|
| 95 |
|
|---|
| 96 | if (match === '*') {
|
|---|
| 97 | backtrack = '';
|
|---|
| 98 | extraOffset += 3;
|
|---|
| 99 | return '(.*)';
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | if (match === '/(') {
|
|---|
| 103 | backtrack += '/';
|
|---|
| 104 | extraOffset += 2;
|
|---|
| 105 | return '/(?:';
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | slash = slash || '';
|
|---|
| 109 | format = format ? '\\.' : '';
|
|---|
| 110 | optional = optional || '';
|
|---|
| 111 | capture = capture ?
|
|---|
| 112 | capture.replace(/\\.|\*/, function (m) { return m === '*' ? '(.*)' : m; }) :
|
|---|
| 113 | (backtrack ? '((?:(?!/|' + backtrack + ').)+?)' : '([^/' + format + ']+?)');
|
|---|
| 114 |
|
|---|
| 115 | keys.push({
|
|---|
| 116 | name: key,
|
|---|
| 117 | optional: !!optional,
|
|---|
| 118 | offset: offset + extraOffset
|
|---|
| 119 | });
|
|---|
| 120 |
|
|---|
| 121 | var result = '(?:'
|
|---|
| 122 | + format + slash + capture
|
|---|
| 123 | + (star ? '((?:[/' + format + '].+?)?)' : '')
|
|---|
| 124 | + ')'
|
|---|
| 125 | + optional;
|
|---|
| 126 |
|
|---|
| 127 | backtrack = '';
|
|---|
| 128 | extraOffset += result.length - match.length;
|
|---|
| 129 |
|
|---|
| 130 | return result;
|
|---|
| 131 | });
|
|---|
| 132 |
|
|---|
| 133 | // This is a workaround for handling unnamed matching groups.
|
|---|
| 134 | while (m = MATCHING_GROUP_REGEXP.exec(path)) {
|
|---|
| 135 | if (m[0][0] === '\\') continue;
|
|---|
| 136 |
|
|---|
| 137 | if (keysOffset + i === keys.length || keys[keysOffset + i].offset > m.index) {
|
|---|
| 138 | keys.splice(keysOffset + i, 0, {
|
|---|
| 139 | name: name++, // Unnamed matching groups must be consistently linear.
|
|---|
| 140 | optional: false,
|
|---|
| 141 | offset: m.index
|
|---|
| 142 | });
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | i++;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | path += strict ? '' : path[path.length - 1] === '/' ? '?' : '/?';
|
|---|
| 149 |
|
|---|
| 150 | // If the path is non-ending, match until the end or a slash.
|
|---|
| 151 | if (end) {
|
|---|
| 152 | path += '$';
|
|---|
| 153 | } else if (path[path.length - 1] !== '/') {
|
|---|
| 154 | path += lookahead ? '(?=/|$)' : '(?:/|$)';
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | return new RegExp('^' + path, flags);
|
|---|
| 158 | };
|
|---|