Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

matchPath.js 2.2 KiB

3 år sedan
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. exports.__esModule = true;
  3. var _pathToRegexp = require("path-to-regexp");
  4. var _pathToRegexp2 = _interopRequireDefault(_pathToRegexp);
  5. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  6. var patternCache = {};
  7. var cacheLimit = 10000;
  8. var cacheCount = 0;
  9. var compilePath = function compilePath(pattern, options) {
  10. var cacheKey = "" + options.end + options.strict + options.sensitive;
  11. var cache = patternCache[cacheKey] || (patternCache[cacheKey] = {});
  12. if (cache[pattern]) return cache[pattern];
  13. var keys = [];
  14. var re = (0, _pathToRegexp2.default)(pattern, keys, options);
  15. var compiledPattern = { re: re, keys: keys };
  16. if (cacheCount < cacheLimit) {
  17. cache[pattern] = compiledPattern;
  18. cacheCount++;
  19. }
  20. return compiledPattern;
  21. };
  22. /**
  23. * Public API for matching a URL pathname to a path pattern.
  24. */
  25. var matchPath = function matchPath(pathname) {
  26. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  27. var parent = arguments[2];
  28. if (typeof options === "string") options = { path: options };
  29. var _options = options,
  30. path = _options.path,
  31. _options$exact = _options.exact,
  32. exact = _options$exact === undefined ? false : _options$exact,
  33. _options$strict = _options.strict,
  34. strict = _options$strict === undefined ? false : _options$strict,
  35. _options$sensitive = _options.sensitive,
  36. sensitive = _options$sensitive === undefined ? false : _options$sensitive;
  37. if (path == null) return parent;
  38. var _compilePath = compilePath(path, { end: exact, strict: strict, sensitive: sensitive }),
  39. re = _compilePath.re,
  40. keys = _compilePath.keys;
  41. var match = re.exec(pathname);
  42. if (!match) return null;
  43. var url = match[0],
  44. values = match.slice(1);
  45. var isExact = pathname === url;
  46. if (exact && !isExact) return null;
  47. return {
  48. path: path, // the path pattern used to match
  49. url: path === "/" && url === "" ? "/" : url, // the matched portion of the URL
  50. isExact: isExact, // whether or not we matched exactly
  51. params: keys.reduce(function (memo, key, index) {
  52. memo[key.name] = values[index];
  53. return memo;
  54. }, {})
  55. };
  56. };
  57. exports.default = matchPath;