You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.3 KiB

  1. /**
  2. * Expose `pathtoRegexp`.
  3. */
  4. module.exports = pathtoRegexp;
  5. /**
  6. * Match matching groups in a regular expression.
  7. */
  8. var MATCHING_GROUP_REGEXP = /\((?!\?)/g;
  9. /**
  10. * Normalize the given path string,
  11. * returning a regular expression.
  12. *
  13. * An empty array should be passed,
  14. * which will contain the placeholder
  15. * key names. For example "/user/:id" will
  16. * then contain ["id"].
  17. *
  18. * @param {String|RegExp|Array} path
  19. * @param {Array} keys
  20. * @param {Object} options
  21. * @return {RegExp}
  22. * @api private
  23. */
  24. function pathtoRegexp(path, keys, options) {
  25. options = options || {};
  26. keys = keys || [];
  27. var strict = options.strict;
  28. var end = options.end !== false;
  29. var flags = options.sensitive ? '' : 'i';
  30. var extraOffset = 0;
  31. var keysOffset = keys.length;
  32. var i = 0;
  33. var name = 0;
  34. var m;
  35. if (path instanceof RegExp) {
  36. while (m = MATCHING_GROUP_REGEXP.exec(path.source)) {
  37. keys.push({
  38. name: name++,
  39. optional: false,
  40. offset: m.index
  41. });
  42. }
  43. return path;
  44. }
  45. if (Array.isArray(path)) {
  46. // Map array parts into regexps and return their source. We also pass
  47. // the same keys and options instance into every generation to get
  48. // consistent matching groups before we join the sources together.
  49. path = path.map(function (value) {
  50. return pathtoRegexp(value, keys, options).source;
  51. });
  52. return new RegExp('(?:' + path.join('|') + ')', flags);
  53. }
  54. path = ('^' + path + (strict ? '' : path[path.length - 1] === '/' ? '?' : '/?'))
  55. .replace(/\/\(/g, '/(?:')
  56. .replace(/([\/\.])/g, '\\$1')
  57. .replace(/(\\\/)?(\\\.)?:(\w+)(\(.*?\))?(\*)?(\?)?/g, function (match, slash, format, key, capture, star, optional, offset) {
  58. slash = slash || '';
  59. format = format || '';
  60. capture = capture || '([^\\/' + format + ']+?)';
  61. optional = optional || '';
  62. keys.push({
  63. name: key,
  64. optional: !!optional,
  65. offset: offset + extraOffset
  66. });
  67. var result = ''
  68. + (optional ? '' : slash)
  69. + '(?:'
  70. + format + (optional ? slash : '') + capture
  71. + (star ? '((?:[\\/' + format + '].+?)?)' : '')
  72. + ')'
  73. + optional;
  74. extraOffset += result.length - match.length;
  75. return result;
  76. })
  77. .replace(/\*/g, function (star, index) {
  78. var len = keys.length
  79. while (len-- > keysOffset && keys[len].offset > index) {
  80. keys[len].offset += 3; // Replacement length minus asterisk length.
  81. }
  82. return '(.*)';
  83. });
  84. // This is a workaround for handling unnamed matching groups.
  85. while (m = MATCHING_GROUP_REGEXP.exec(path)) {
  86. var escapeCount = 0;
  87. var index = m.index;
  88. while (path.charAt(--index) === '\\') {
  89. escapeCount++;
  90. }
  91. // It's possible to escape the bracket.
  92. if (escapeCount % 2 === 1) {
  93. continue;
  94. }
  95. if (keysOffset + i === keys.length || keys[keysOffset + i].offset > m.index) {
  96. keys.splice(keysOffset + i, 0, {
  97. name: name++, // Unnamed matching groups must be consistently linear.
  98. optional: false,
  99. offset: m.index
  100. });
  101. }
  102. i++;
  103. }
  104. // If the path is non-ending, match until the end or a slash.
  105. path += (end ? '$' : (path[path.length - 1] === '/' ? '' : '(?=\\/|$)'));
  106. return new RegExp(path, flags);
  107. };