path-match.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getPathMatch = getPathMatch;
  6. var _extends = require("@swc/helpers/lib/_extends.js").default;
  7. var _pathToRegexp = require("next/dist/compiled/path-to-regexp");
  8. function getPathMatch(path, options) {
  9. const keys = [];
  10. const regexp = (0, _pathToRegexp).pathToRegexp(path, keys, {
  11. delimiter: '/',
  12. sensitive: false,
  13. strict: options == null ? void 0 : options.strict
  14. });
  15. const matcher = (0, _pathToRegexp).regexpToFunction((options == null ? void 0 : options.regexModifier) ? new RegExp(options.regexModifier(regexp.source), regexp.flags) : regexp, keys);
  16. /**
  17. * A matcher function that will check if a given pathname matches the path
  18. * given in the builder function. When the path does not match it will return
  19. * `false` but if it does it will return an object with the matched params
  20. * merged with the params provided in the second argument.
  21. */ return (pathname, params)=>{
  22. const res = pathname == null ? false : matcher(pathname);
  23. if (!res) {
  24. return false;
  25. }
  26. /**
  27. * If unnamed params are not allowed they must be removed from
  28. * the matched parameters. path-to-regexp uses "string" for named and
  29. * "number" for unnamed parameters.
  30. */ if (options == null ? void 0 : options.removeUnnamedParams) {
  31. for (const key of keys){
  32. if (typeof key.name === 'number') {
  33. delete res.params[key.name];
  34. }
  35. }
  36. }
  37. return _extends({}, params, res.params);
  38. };
  39. }
  40. //# sourceMappingURL=path-match.js.map