index.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /**
  2. * Expose `pathToRegexp`.
  3. */
  4. module.exports = pathToRegexp
  5. module.exports.parse = parse
  6. module.exports.compile = compile
  7. module.exports.tokensToFunction = tokensToFunction
  8. module.exports.tokensToRegExp = tokensToRegExp
  9. /**
  10. * Default configs.
  11. */
  12. var DEFAULT_DELIMITER = '/'
  13. var DEFAULT_DELIMITERS = './'
  14. /**
  15. * The main path matching regexp utility.
  16. *
  17. * @type {RegExp}
  18. */
  19. var PATH_REGEXP = new RegExp([
  20. // Match escaped characters that would otherwise appear in future matches.
  21. // This allows the user to escape special characters that won't transform.
  22. '(\\\\.)',
  23. // Match Express-style parameters and un-named parameters with a prefix
  24. // and optional suffixes. Matches appear as:
  25. //
  26. // ":test(\\d+)?" => ["test", "\d+", undefined, "?"]
  27. // "(\\d+)" => [undefined, undefined, "\d+", undefined]
  28. '(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?'
  29. ].join('|'), 'g')
  30. /**
  31. * Parse a string for the raw tokens.
  32. *
  33. * @param {string} str
  34. * @param {Object=} options
  35. * @return {!Array}
  36. */
  37. function parse (str, options) {
  38. var tokens = []
  39. var key = 0
  40. var index = 0
  41. var path = ''
  42. var defaultDelimiter = (options && options.delimiter) || DEFAULT_DELIMITER
  43. var delimiters = (options && options.delimiters) || DEFAULT_DELIMITERS
  44. var pathEscaped = false
  45. var res
  46. while ((res = PATH_REGEXP.exec(str)) !== null) {
  47. var m = res[0]
  48. var escaped = res[1]
  49. var offset = res.index
  50. path += str.slice(index, offset)
  51. index = offset + m.length
  52. // Ignore already escaped sequences.
  53. if (escaped) {
  54. path += escaped[1]
  55. pathEscaped = true
  56. continue
  57. }
  58. var prev = ''
  59. var next = str[index]
  60. var name = res[2]
  61. var capture = res[3]
  62. var group = res[4]
  63. var modifier = res[5]
  64. if (!pathEscaped && path.length) {
  65. var k = path.length - 1
  66. if (delimiters.indexOf(path[k]) > -1) {
  67. prev = path[k]
  68. path = path.slice(0, k)
  69. }
  70. }
  71. // Push the current path onto the tokens.
  72. if (path) {
  73. tokens.push(path)
  74. path = ''
  75. pathEscaped = false
  76. }
  77. var partial = prev !== '' && next !== undefined && next !== prev
  78. var repeat = modifier === '+' || modifier === '*'
  79. var optional = modifier === '?' || modifier === '*'
  80. var delimiter = prev || defaultDelimiter
  81. var pattern = capture || group
  82. tokens.push({
  83. name: name || key++,
  84. prefix: prev,
  85. delimiter: delimiter,
  86. optional: optional,
  87. repeat: repeat,
  88. partial: partial,
  89. pattern: pattern ? escapeGroup(pattern) : '[^' + escapeString(delimiter) + ']+?'
  90. })
  91. }
  92. // Push any remaining characters.
  93. if (path || index < str.length) {
  94. tokens.push(path + str.substr(index))
  95. }
  96. return tokens
  97. }
  98. /**
  99. * Compile a string to a template function for the path.
  100. *
  101. * @param {string} str
  102. * @param {Object=} options
  103. * @return {!function(Object=, Object=)}
  104. */
  105. function compile (str, options) {
  106. return tokensToFunction(parse(str, options))
  107. }
  108. /**
  109. * Expose a method for transforming tokens into the path function.
  110. */
  111. function tokensToFunction (tokens) {
  112. // Compile all the tokens into regexps.
  113. var matches = new Array(tokens.length)
  114. // Compile all the patterns before compilation.
  115. for (var i = 0; i < tokens.length; i++) {
  116. if (typeof tokens[i] === 'object') {
  117. matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$')
  118. }
  119. }
  120. return function (data, options) {
  121. var path = ''
  122. var encode = (options && options.encode) || encodeURIComponent
  123. for (var i = 0; i < tokens.length; i++) {
  124. var token = tokens[i]
  125. if (typeof token === 'string') {
  126. path += token
  127. continue
  128. }
  129. var value = data ? data[token.name] : undefined
  130. var segment
  131. if (Array.isArray(value)) {
  132. if (!token.repeat) {
  133. throw new TypeError('Expected "' + token.name + '" to not repeat, but got array')
  134. }
  135. if (value.length === 0) {
  136. if (token.optional) continue
  137. throw new TypeError('Expected "' + token.name + '" to not be empty')
  138. }
  139. for (var j = 0; j < value.length; j++) {
  140. segment = encode(value[j], token)
  141. if (!matches[i].test(segment)) {
  142. throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '"')
  143. }
  144. path += (j === 0 ? token.prefix : token.delimiter) + segment
  145. }
  146. continue
  147. }
  148. if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
  149. segment = encode(String(value), token)
  150. if (!matches[i].test(segment)) {
  151. throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '", but got "' + segment + '"')
  152. }
  153. path += token.prefix + segment
  154. continue
  155. }
  156. if (token.optional) {
  157. // Prepend partial segment prefixes.
  158. if (token.partial) path += token.prefix
  159. continue
  160. }
  161. throw new TypeError('Expected "' + token.name + '" to be ' + (token.repeat ? 'an array' : 'a string'))
  162. }
  163. return path
  164. }
  165. }
  166. /**
  167. * Escape a regular expression string.
  168. *
  169. * @param {string} str
  170. * @return {string}
  171. */
  172. function escapeString (str) {
  173. return str.replace(/([.+*?=^!:${}()[\]|/\\])/g, '\\$1')
  174. }
  175. /**
  176. * Escape the capturing group by escaping special characters and meaning.
  177. *
  178. * @param {string} group
  179. * @return {string}
  180. */
  181. function escapeGroup (group) {
  182. return group.replace(/([=!:$/()])/g, '\\$1')
  183. }
  184. /**
  185. * Get the flags for a regexp from the options.
  186. *
  187. * @param {Object} options
  188. * @return {string}
  189. */
  190. function flags (options) {
  191. return options && options.sensitive ? '' : 'i'
  192. }
  193. /**
  194. * Pull out keys from a regexp.
  195. *
  196. * @param {!RegExp} path
  197. * @param {Array=} keys
  198. * @return {!RegExp}
  199. */
  200. function regexpToRegexp (path, keys) {
  201. if (!keys) return path
  202. // Use a negative lookahead to match only capturing groups.
  203. var groups = path.source.match(/\((?!\?)/g)
  204. if (groups) {
  205. for (var i = 0; i < groups.length; i++) {
  206. keys.push({
  207. name: i,
  208. prefix: null,
  209. delimiter: null,
  210. optional: false,
  211. repeat: false,
  212. partial: false,
  213. pattern: null
  214. })
  215. }
  216. }
  217. return path
  218. }
  219. /**
  220. * Transform an array into a regexp.
  221. *
  222. * @param {!Array} path
  223. * @param {Array=} keys
  224. * @param {Object=} options
  225. * @return {!RegExp}
  226. */
  227. function arrayToRegexp (path, keys, options) {
  228. var parts = []
  229. for (var i = 0; i < path.length; i++) {
  230. parts.push(pathToRegexp(path[i], keys, options).source)
  231. }
  232. return new RegExp('(?:' + parts.join('|') + ')', flags(options))
  233. }
  234. /**
  235. * Create a path regexp from string input.
  236. *
  237. * @param {string} path
  238. * @param {Array=} keys
  239. * @param {Object=} options
  240. * @return {!RegExp}
  241. */
  242. function stringToRegexp (path, keys, options) {
  243. return tokensToRegExp(parse(path, options), keys, options)
  244. }
  245. /**
  246. * Expose a function for taking tokens and returning a RegExp.
  247. *
  248. * @param {!Array} tokens
  249. * @param {Array=} keys
  250. * @param {Object=} options
  251. * @return {!RegExp}
  252. */
  253. function tokensToRegExp (tokens, keys, options) {
  254. options = options || {}
  255. var strict = options.strict
  256. var start = options.start !== false
  257. var end = options.end !== false
  258. var delimiter = escapeString(options.delimiter || DEFAULT_DELIMITER)
  259. var delimiters = options.delimiters || DEFAULT_DELIMITERS
  260. var endsWith = [].concat(options.endsWith || []).map(escapeString).concat('$').join('|')
  261. var route = start ? '^' : ''
  262. var isEndDelimited = tokens.length === 0
  263. // Iterate over the tokens and create our regexp string.
  264. for (var i = 0; i < tokens.length; i++) {
  265. var token = tokens[i]
  266. if (typeof token === 'string') {
  267. route += escapeString(token)
  268. isEndDelimited = i === tokens.length - 1 && delimiters.indexOf(token[token.length - 1]) > -1
  269. } else {
  270. var capture = token.repeat
  271. ? '(?:' + token.pattern + ')(?:' + escapeString(token.delimiter) + '(?:' + token.pattern + '))*'
  272. : token.pattern
  273. if (keys) keys.push(token)
  274. if (token.optional) {
  275. if (token.partial) {
  276. route += escapeString(token.prefix) + '(' + capture + ')?'
  277. } else {
  278. route += '(?:' + escapeString(token.prefix) + '(' + capture + '))?'
  279. }
  280. } else {
  281. route += escapeString(token.prefix) + '(' + capture + ')'
  282. }
  283. }
  284. }
  285. if (end) {
  286. if (!strict) route += '(?:' + delimiter + ')?'
  287. route += endsWith === '$' ? '$' : '(?=' + endsWith + ')'
  288. } else {
  289. if (!strict) route += '(?:' + delimiter + '(?=' + endsWith + '))?'
  290. if (!isEndDelimited) route += '(?=' + delimiter + '|' + endsWith + ')'
  291. }
  292. return new RegExp(route, flags(options))
  293. }
  294. /**
  295. * Normalize the given path string, returning a regular expression.
  296. *
  297. * An empty array can be passed in for the keys, which will hold the
  298. * placeholder key descriptions. For example, using `/user/:id`, `keys` will
  299. * contain `[{ name: 'id', delimiter: '/', optional: false, repeat: false }]`.
  300. *
  301. * @param {(string|RegExp|Array)} path
  302. * @param {Array=} keys
  303. * @param {Object=} options
  304. * @return {!RegExp}
  305. */
  306. function pathToRegexp (path, keys, options) {
  307. if (path instanceof RegExp) {
  308. return regexpToRegexp(path, keys)
  309. }
  310. if (Array.isArray(path)) {
  311. return arrayToRegexp(/** @type {!Array} */ (path), keys, options)
  312. }
  313. return stringToRegexp(/** @type {string} */ (path), keys, options)
  314. }