2024-10-14 06:09:33 +00:00
|
|
|
/**
|
|
|
|
* Expose `pathToRegexp`.
|
|
|
|
*/
|
|
|
|
module.exports = pathToRegexp
|
2024-10-16 10:36:51 +00:00
|
|
|
module.exports.match = match
|
|
|
|
module.exports.regexpToFunction = regexpToFunction
|
2024-10-14 06:09:33 +00:00
|
|
|
module.exports.parse = parse
|
|
|
|
module.exports.compile = compile
|
|
|
|
module.exports.tokensToFunction = tokensToFunction
|
|
|
|
module.exports.tokensToRegExp = tokensToRegExp
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default configs.
|
|
|
|
*/
|
|
|
|
var DEFAULT_DELIMITER = '/'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The main path matching regexp utility.
|
|
|
|
*
|
|
|
|
* @type {RegExp}
|
|
|
|
*/
|
|
|
|
var PATH_REGEXP = new RegExp([
|
|
|
|
// Match escaped characters that would otherwise appear in future matches.
|
|
|
|
// This allows the user to escape special characters that won't transform.
|
|
|
|
'(\\\\.)',
|
|
|
|
// Match Express-style parameters and un-named parameters with a prefix
|
|
|
|
// and optional suffixes. Matches appear as:
|
|
|
|
//
|
2024-10-16 10:36:51 +00:00
|
|
|
// ":test(\\d+)?" => ["test", "\d+", undefined, "?"]
|
|
|
|
// "(\\d+)" => [undefined, undefined, "\d+", undefined]
|
2024-10-14 06:09:33 +00:00
|
|
|
'(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?'
|
|
|
|
].join('|'), 'g')
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a string for the raw tokens.
|
|
|
|
*
|
|
|
|
* @param {string} str
|
|
|
|
* @param {Object=} options
|
|
|
|
* @return {!Array}
|
|
|
|
*/
|
|
|
|
function parse (str, options) {
|
|
|
|
var tokens = []
|
|
|
|
var key = 0
|
|
|
|
var index = 0
|
|
|
|
var path = ''
|
|
|
|
var defaultDelimiter = (options && options.delimiter) || DEFAULT_DELIMITER
|
2024-10-16 10:36:51 +00:00
|
|
|
var whitelist = (options && options.whitelist) || undefined
|
2024-10-14 06:09:33 +00:00
|
|
|
var pathEscaped = false
|
|
|
|
var res
|
|
|
|
|
|
|
|
while ((res = PATH_REGEXP.exec(str)) !== null) {
|
|
|
|
var m = res[0]
|
|
|
|
var escaped = res[1]
|
|
|
|
var offset = res.index
|
|
|
|
path += str.slice(index, offset)
|
|
|
|
index = offset + m.length
|
|
|
|
|
|
|
|
// Ignore already escaped sequences.
|
|
|
|
if (escaped) {
|
|
|
|
path += escaped[1]
|
|
|
|
pathEscaped = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var prev = ''
|
|
|
|
var name = res[2]
|
|
|
|
var capture = res[3]
|
|
|
|
var group = res[4]
|
|
|
|
var modifier = res[5]
|
|
|
|
|
|
|
|
if (!pathEscaped && path.length) {
|
|
|
|
var k = path.length - 1
|
2024-10-16 10:36:51 +00:00
|
|
|
var c = path[k]
|
|
|
|
var matches = whitelist ? whitelist.indexOf(c) > -1 : true
|
2024-10-14 06:09:33 +00:00
|
|
|
|
2024-10-16 10:36:51 +00:00
|
|
|
if (matches) {
|
|
|
|
prev = c
|
2024-10-14 06:09:33 +00:00
|
|
|
path = path.slice(0, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the current path onto the tokens.
|
|
|
|
if (path) {
|
|
|
|
tokens.push(path)
|
|
|
|
path = ''
|
|
|
|
pathEscaped = false
|
|
|
|
}
|
|
|
|
|
|
|
|
var repeat = modifier === '+' || modifier === '*'
|
|
|
|
var optional = modifier === '?' || modifier === '*'
|
|
|
|
var pattern = capture || group
|
2024-10-16 10:36:51 +00:00
|
|
|
var delimiter = prev || defaultDelimiter
|
|
|
|
var prevText = prev || (typeof tokens[tokens.length - 1] === 'string' ? tokens[tokens.length - 1] : '')
|
2024-10-14 06:09:33 +00:00
|
|
|
|
|
|
|
tokens.push({
|
|
|
|
name: name || key++,
|
|
|
|
prefix: prev,
|
|
|
|
delimiter: delimiter,
|
|
|
|
optional: optional,
|
|
|
|
repeat: repeat,
|
2024-10-16 10:36:51 +00:00
|
|
|
pattern: pattern
|
|
|
|
? escapeGroup(pattern)
|
|
|
|
: restrictBacktrack(delimiter, defaultDelimiter, prevText)
|
2024-10-14 06:09:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push any remaining characters.
|
|
|
|
if (path || index < str.length) {
|
|
|
|
tokens.push(path + str.substr(index))
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokens
|
|
|
|
}
|
|
|
|
|
2024-10-16 10:36:51 +00:00
|
|
|
function restrictBacktrack (delimiter, defaultDelimiter, prevText) {
|
|
|
|
var charGroup = '[^' + escapeString(delimiter === defaultDelimiter ? delimiter : (delimiter + defaultDelimiter)) + ']'
|
|
|
|
|
|
|
|
if (!prevText || prevText.indexOf(delimiter) > -1 || prevText.indexOf(defaultDelimiter) > -1) {
|
|
|
|
return charGroup + '+?'
|
|
|
|
}
|
|
|
|
|
|
|
|
return escapeString(prevText) + '|(?:(?!' + escapeString(prevText) + ')' + charGroup + ')+?'
|
|
|
|
}
|
|
|
|
|
2024-10-14 06:09:33 +00:00
|
|
|
/**
|
|
|
|
* Compile a string to a template function for the path.
|
|
|
|
*
|
|
|
|
* @param {string} str
|
|
|
|
* @param {Object=} options
|
|
|
|
* @return {!function(Object=, Object=)}
|
|
|
|
*/
|
|
|
|
function compile (str, options) {
|
2024-10-16 10:36:51 +00:00
|
|
|
return tokensToFunction(parse(str, options), options)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create path match function from `path-to-regexp` spec.
|
|
|
|
*/
|
|
|
|
function match (str, options) {
|
|
|
|
var keys = []
|
|
|
|
var re = pathToRegexp(str, keys, options)
|
|
|
|
return regexpToFunction(re, keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a path match function from `path-to-regexp` output.
|
|
|
|
*/
|
|
|
|
function regexpToFunction (re, keys) {
|
|
|
|
return function (pathname, options) {
|
|
|
|
var m = re.exec(pathname)
|
|
|
|
if (!m) return false
|
|
|
|
|
|
|
|
var path = m[0]
|
|
|
|
var index = m.index
|
|
|
|
var params = {}
|
|
|
|
var decode = (options && options.decode) || decodeURIComponent
|
|
|
|
|
|
|
|
for (var i = 1; i < m.length; i++) {
|
|
|
|
if (m[i] === undefined) continue
|
|
|
|
|
|
|
|
var key = keys[i - 1]
|
|
|
|
|
|
|
|
if (key.repeat) {
|
|
|
|
params[key.name] = m[i].split(key.delimiter).map(function (value) {
|
|
|
|
return decode(value, key)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
params[key.name] = decode(m[i], key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { path: path, index: index, params: params }
|
|
|
|
}
|
2024-10-14 06:09:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expose a method for transforming tokens into the path function.
|
|
|
|
*/
|
2024-10-16 10:36:51 +00:00
|
|
|
function tokensToFunction (tokens, options) {
|
2024-10-14 06:09:33 +00:00
|
|
|
// Compile all the tokens into regexps.
|
|
|
|
var matches = new Array(tokens.length)
|
|
|
|
|
|
|
|
// Compile all the patterns before compilation.
|
|
|
|
for (var i = 0; i < tokens.length; i++) {
|
|
|
|
if (typeof tokens[i] === 'object') {
|
2024-10-16 10:36:51 +00:00
|
|
|
matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$', flags(options))
|
2024-10-14 06:09:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return function (data, options) {
|
|
|
|
var path = ''
|
|
|
|
var encode = (options && options.encode) || encodeURIComponent
|
2024-10-16 10:36:51 +00:00
|
|
|
var validate = options ? options.validate !== false : true
|
2024-10-14 06:09:33 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < tokens.length; i++) {
|
|
|
|
var token = tokens[i]
|
|
|
|
|
|
|
|
if (typeof token === 'string') {
|
|
|
|
path += token
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var value = data ? data[token.name] : undefined
|
|
|
|
var segment
|
|
|
|
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
if (!token.repeat) {
|
|
|
|
throw new TypeError('Expected "' + token.name + '" to not repeat, but got array')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value.length === 0) {
|
|
|
|
if (token.optional) continue
|
|
|
|
|
|
|
|
throw new TypeError('Expected "' + token.name + '" to not be empty')
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var j = 0; j < value.length; j++) {
|
|
|
|
segment = encode(value[j], token)
|
|
|
|
|
2024-10-16 10:36:51 +00:00
|
|
|
if (validate && !matches[i].test(segment)) {
|
2024-10-14 06:09:33 +00:00
|
|
|
throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '"')
|
|
|
|
}
|
|
|
|
|
|
|
|
path += (j === 0 ? token.prefix : token.delimiter) + segment
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
|
|
segment = encode(String(value), token)
|
|
|
|
|
2024-10-16 10:36:51 +00:00
|
|
|
if (validate && !matches[i].test(segment)) {
|
2024-10-14 06:09:33 +00:00
|
|
|
throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '", but got "' + segment + '"')
|
|
|
|
}
|
|
|
|
|
|
|
|
path += token.prefix + segment
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-10-16 10:36:51 +00:00
|
|
|
if (token.optional) continue
|
2024-10-14 06:09:33 +00:00
|
|
|
|
|
|
|
throw new TypeError('Expected "' + token.name + '" to be ' + (token.repeat ? 'an array' : 'a string'))
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape a regular expression string.
|
|
|
|
*
|
|
|
|
* @param {string} str
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function escapeString (str) {
|
|
|
|
return str.replace(/([.+*?=^!:${}()[\]|/\\])/g, '\\$1')
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape the capturing group by escaping special characters and meaning.
|
|
|
|
*
|
|
|
|
* @param {string} group
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function escapeGroup (group) {
|
|
|
|
return group.replace(/([=!:$/()])/g, '\\$1')
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the flags for a regexp from the options.
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function flags (options) {
|
|
|
|
return options && options.sensitive ? '' : 'i'
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pull out keys from a regexp.
|
|
|
|
*
|
|
|
|
* @param {!RegExp} path
|
|
|
|
* @param {Array=} keys
|
|
|
|
* @return {!RegExp}
|
|
|
|
*/
|
|
|
|
function regexpToRegexp (path, keys) {
|
|
|
|
if (!keys) return path
|
|
|
|
|
|
|
|
// Use a negative lookahead to match only capturing groups.
|
|
|
|
var groups = path.source.match(/\((?!\?)/g)
|
|
|
|
|
|
|
|
if (groups) {
|
|
|
|
for (var i = 0; i < groups.length; i++) {
|
|
|
|
keys.push({
|
|
|
|
name: i,
|
|
|
|
prefix: null,
|
|
|
|
delimiter: null,
|
|
|
|
optional: false,
|
|
|
|
repeat: false,
|
|
|
|
pattern: null
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform an array into a regexp.
|
|
|
|
*
|
|
|
|
* @param {!Array} path
|
|
|
|
* @param {Array=} keys
|
|
|
|
* @param {Object=} options
|
|
|
|
* @return {!RegExp}
|
|
|
|
*/
|
|
|
|
function arrayToRegexp (path, keys, options) {
|
|
|
|
var parts = []
|
|
|
|
|
|
|
|
for (var i = 0; i < path.length; i++) {
|
|
|
|
parts.push(pathToRegexp(path[i], keys, options).source)
|
|
|
|
}
|
|
|
|
|
|
|
|
return new RegExp('(?:' + parts.join('|') + ')', flags(options))
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a path regexp from string input.
|
|
|
|
*
|
|
|
|
* @param {string} path
|
|
|
|
* @param {Array=} keys
|
|
|
|
* @param {Object=} options
|
|
|
|
* @return {!RegExp}
|
|
|
|
*/
|
|
|
|
function stringToRegexp (path, keys, options) {
|
|
|
|
return tokensToRegExp(parse(path, options), keys, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expose a function for taking tokens and returning a RegExp.
|
|
|
|
*
|
|
|
|
* @param {!Array} tokens
|
|
|
|
* @param {Array=} keys
|
|
|
|
* @param {Object=} options
|
|
|
|
* @return {!RegExp}
|
|
|
|
*/
|
|
|
|
function tokensToRegExp (tokens, keys, options) {
|
|
|
|
options = options || {}
|
|
|
|
|
|
|
|
var strict = options.strict
|
2024-10-16 10:36:51 +00:00
|
|
|
var start = options.start !== false
|
2024-10-14 06:09:33 +00:00
|
|
|
var end = options.end !== false
|
2024-10-16 10:36:51 +00:00
|
|
|
var delimiter = options.delimiter || DEFAULT_DELIMITER
|
2024-10-14 06:09:33 +00:00
|
|
|
var endsWith = [].concat(options.endsWith || []).map(escapeString).concat('$').join('|')
|
2024-10-16 10:36:51 +00:00
|
|
|
var route = start ? '^' : ''
|
2024-10-14 06:09:33 +00:00
|
|
|
|
|
|
|
// Iterate over the tokens and create our regexp string.
|
|
|
|
for (var i = 0; i < tokens.length; i++) {
|
|
|
|
var token = tokens[i]
|
|
|
|
|
|
|
|
if (typeof token === 'string') {
|
|
|
|
route += escapeString(token)
|
|
|
|
} else {
|
|
|
|
var capture = token.repeat
|
2024-10-16 10:36:51 +00:00
|
|
|
? '(?:' + token.pattern + ')(?:' + escapeString(token.delimiter) + '(?:' + token.pattern + '))*'
|
2024-10-14 06:09:33 +00:00
|
|
|
: token.pattern
|
|
|
|
|
|
|
|
if (keys) keys.push(token)
|
|
|
|
|
|
|
|
if (token.optional) {
|
2024-10-16 10:36:51 +00:00
|
|
|
if (!token.prefix) {
|
|
|
|
route += '(' + capture + ')?'
|
2024-10-14 06:09:33 +00:00
|
|
|
} else {
|
2024-10-16 10:36:51 +00:00
|
|
|
route += '(?:' + escapeString(token.prefix) + '(' + capture + '))?'
|
2024-10-14 06:09:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-10-16 10:36:51 +00:00
|
|
|
route += escapeString(token.prefix) + '(' + capture + ')'
|
2024-10-14 06:09:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end) {
|
2024-10-16 10:36:51 +00:00
|
|
|
if (!strict) route += '(?:' + escapeString(delimiter) + ')?'
|
2024-10-14 06:09:33 +00:00
|
|
|
|
|
|
|
route += endsWith === '$' ? '$' : '(?=' + endsWith + ')'
|
|
|
|
} else {
|
2024-10-16 10:36:51 +00:00
|
|
|
var endToken = tokens[tokens.length - 1]
|
|
|
|
var isEndDelimited = typeof endToken === 'string'
|
|
|
|
? endToken[endToken.length - 1] === delimiter
|
|
|
|
: endToken === undefined
|
|
|
|
|
|
|
|
if (!strict) route += '(?:' + escapeString(delimiter) + '(?=' + endsWith + '))?'
|
|
|
|
if (!isEndDelimited) route += '(?=' + escapeString(delimiter) + '|' + endsWith + ')'
|
2024-10-14 06:09:33 +00:00
|
|
|
}
|
|
|
|
|
2024-10-16 10:36:51 +00:00
|
|
|
return new RegExp(route, flags(options))
|
2024-10-14 06:09:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize the given path string, returning a regular expression.
|
|
|
|
*
|
|
|
|
* An empty array can be passed in for the keys, which will hold the
|
|
|
|
* placeholder key descriptions. For example, using `/user/:id`, `keys` will
|
|
|
|
* contain `[{ name: 'id', delimiter: '/', optional: false, repeat: false }]`.
|
|
|
|
*
|
|
|
|
* @param {(string|RegExp|Array)} path
|
|
|
|
* @param {Array=} keys
|
|
|
|
* @param {Object=} options
|
|
|
|
* @return {!RegExp}
|
|
|
|
*/
|
|
|
|
function pathToRegexp (path, keys, options) {
|
|
|
|
if (path instanceof RegExp) {
|
|
|
|
return regexpToRegexp(path, keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(path)) {
|
|
|
|
return arrayToRegexp(/** @type {!Array} */ (path), keys, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringToRegexp(/** @type {string} */ (path), keys, options)
|
|
|
|
}
|