Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

matchPath.js 2.0 KiB

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