123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- var isarray = require('isarray')
- module.exports = pathToRegexp
- module.exports.parse = parse
- module.exports.compile = compile
- module.exports.tokensToFunction = tokensToFunction
- module.exports.tokensToRegExp = tokensToRegExp
- var PATH_REGEXP = new RegExp([
-
-
- '(\\\\.)',
-
-
-
-
-
-
- '([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?|(\\*))'
- ].join('|'), 'g')
- function parse (str, options) {
- var tokens = []
- var key = 0
- var index = 0
- var path = ''
- var defaultDelimiter = options && options.delimiter || '/'
- 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
-
- if (escaped) {
- path += escaped[1]
- continue
- }
- var next = str[index]
- var prefix = res[2]
- var name = res[3]
- var capture = res[4]
- var group = res[5]
- var modifier = res[6]
- var asterisk = res[7]
-
- if (path) {
- tokens.push(path)
- path = ''
- }
- var partial = prefix != null && next != null && next !== prefix
- var repeat = modifier === '+' || modifier === '*'
- var optional = modifier === '?' || modifier === '*'
- var delimiter = res[2] || defaultDelimiter
- var pattern = capture || group
- tokens.push({
- name: name || key++,
- prefix: prefix || '',
- delimiter: delimiter,
- optional: optional,
- repeat: repeat,
- partial: partial,
- asterisk: !!asterisk,
- pattern: pattern ? escapeGroup(pattern) : (asterisk ? '.*' : '[^' + escapeString(delimiter) + ']+?')
- })
- }
-
- if (index < str.length) {
- path += str.substr(index)
- }
-
- if (path) {
- tokens.push(path)
- }
- return tokens
- }
- function compile (str, options) {
- return tokensToFunction(parse(str, options), options)
- }
- function encodeURIComponentPretty (str) {
- return encodeURI(str).replace(/[\/?#]/g, function (c) {
- return '%' + c.charCodeAt(0).toString(16).toUpperCase()
- })
- }
- function encodeAsterisk (str) {
- return encodeURI(str).replace(/[?#]/g, function (c) {
- return '%' + c.charCodeAt(0).toString(16).toUpperCase()
- })
- }
- function tokensToFunction (tokens, options) {
-
- var matches = new Array(tokens.length)
-
- for (var i = 0; i < tokens.length; i++) {
- if (typeof tokens[i] === 'object') {
- matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$', flags(options))
- }
- }
- return function (obj, opts) {
- var path = ''
- var data = obj || {}
- var options = opts || {}
- var encode = options.pretty ? encodeURIComponentPretty : encodeURIComponent
- for (var i = 0; i < tokens.length; i++) {
- var token = tokens[i]
- if (typeof token === 'string') {
- path += token
- continue
- }
- var value = data[token.name]
- var segment
- if (value == null) {
- if (token.optional) {
-
- if (token.partial) {
- path += token.prefix
- }
- continue
- } else {
- throw new TypeError('Expected "' + token.name + '" to be defined')
- }
- }
- if (isarray(value)) {
- if (!token.repeat) {
- throw new TypeError('Expected "' + token.name + '" to not repeat, but received `' + JSON.stringify(value) + '`')
- }
- if (value.length === 0) {
- if (token.optional) {
- continue
- } else {
- throw new TypeError('Expected "' + token.name + '" to not be empty')
- }
- }
- for (var j = 0; j < value.length; j++) {
- segment = encode(value[j])
- if (!matches[i].test(segment)) {
- throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '", but received `' + JSON.stringify(segment) + '`')
- }
- path += (j === 0 ? token.prefix : token.delimiter) + segment
- }
- continue
- }
- segment = token.asterisk ? encodeAsterisk(value) : encode(value)
- if (!matches[i].test(segment)) {
- throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '", but received "' + segment + '"')
- }
- path += token.prefix + segment
- }
- return path
- }
- }
- function escapeString (str) {
- return str.replace(/([.+*?=^!:${}()[\]|\/\\])/g, '\\$1')
- }
- function escapeGroup (group) {
- return group.replace(/([=!:$\/()])/g, '\\$1')
- }
- function attachKeys (re, keys) {
- re.keys = keys
- return re
- }
- function flags (options) {
- return options && options.sensitive ? '' : 'i'
- }
- function regexpToRegexp (path, keys) {
-
- 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,
- partial: false,
- asterisk: false,
- pattern: null
- })
- }
- }
- return attachKeys(path, keys)
- }
- function arrayToRegexp (path, keys, options) {
- var parts = []
- for (var i = 0; i < path.length; i++) {
- parts.push(pathToRegexp(path[i], keys, options).source)
- }
- var regexp = new RegExp('(?:' + parts.join('|') + ')', flags(options))
- return attachKeys(regexp, keys)
- }
- function stringToRegexp (path, keys, options) {
- return tokensToRegExp(parse(path, options), keys, options)
- }
- function tokensToRegExp (tokens, keys, options) {
- if (!isarray(keys)) {
- options = (keys || options)
- keys = []
- }
- options = options || {}
- var strict = options.strict
- var end = options.end !== false
- var route = ''
-
- for (var i = 0; i < tokens.length; i++) {
- var token = tokens[i]
- if (typeof token === 'string') {
- route += escapeString(token)
- } else {
- var prefix = escapeString(token.prefix)
- var capture = '(?:' + token.pattern + ')'
- keys.push(token)
- if (token.repeat) {
- capture += '(?:' + prefix + capture + ')*'
- }
- if (token.optional) {
- if (!token.partial) {
- capture = '(?:' + prefix + '(' + capture + '))?'
- } else {
- capture = prefix + '(' + capture + ')?'
- }
- } else {
- capture = prefix + '(' + capture + ')'
- }
- route += capture
- }
- }
- var delimiter = escapeString(options.delimiter || '/')
- var endsWithDelimiter = route.slice(-delimiter.length) === delimiter
-
-
-
-
- if (!strict) {
- route = (endsWithDelimiter ? route.slice(0, -delimiter.length) : route) + '(?:' + delimiter + '(?=$))?'
- }
- if (end) {
- route += '$'
- } else {
-
-
- route += strict && endsWithDelimiter ? '' : '(?=' + delimiter + '|$)'
- }
- return attachKeys(new RegExp('^' + route, flags(options)), keys)
- }
- function pathToRegexp (path, keys, options) {
- if (!isarray(keys)) {
- options = (keys || options)
- keys = []
- }
- options = options || {}
- if (path instanceof RegExp) {
- return regexpToRegexp(path, (keys))
- }
- if (isarray(path)) {
- return arrayToRegexp( (path), (keys), options)
- }
- return stringToRegexp( (path), (keys), options)
- }
|