The LM Control website. Simple yet efficient.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

index.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. };