/** * Expose `pathtoRegexp`. */ module.exports = pathtoRegexp; /** * Normalize the given path string, * returning a regular expression. * * An empty array should be passed, * which will contain the placeholder * key names. For example "/user/:id" will * then contain ["id"]. * * @param {String|RegExp|Array} path * @param {Array} keys * @param {Object} options * @return {RegExp} * @api private */ function pathtoRegexp(path, keys, options) { options = options || {}; var strict = options.strict; var end = options.end !== false; var flags = options.sensitive ? '' : 'i'; keys = keys || []; if (path instanceof RegExp) { return path; } if (Array.isArray(path)) { // Map array parts into regexps and return their source. We also pass // the same keys and options instance into every generation to get // consistent matching groups before we join the sources together. path = path.map(function (value) { return pathtoRegexp(value, keys, options).source; }); return new RegExp('(?:' + path.join('|') + ')', flags); } path = ('^' + path + (strict ? '' : path[path.length - 1] === '/' ? '?' : '/?')) .replace(/\/\(/g, '/(?:') .replace(/([\/\.])/g, '\\$1') .replace(/(\\\/)?(\\\.)?:(\w+)(\(.*?\))?(\*)?(\?)?/g, function (match, slash, format, key, capture, star, optional) { slash = slash || ''; format = format || ''; capture = capture || '([^\\/' + format + ']+?)'; optional = optional || ''; keys.push({ name: key, optional: !!optional }); return '' + (optional ? '' : slash) + '(?:' + format + (optional ? slash : '') + capture + (star ? '((?:[\\/' + format + '].+?)?)' : '') + ')' + optional; }) .replace(/\*/g, '(.*)'); // If the path is non-ending, match until the end or a slash. path += (end ? '$' : (path[path.length - 1] === '/' ? '' : '(?=\\/|$)')); return new RegExp(path, flags); };