route-matcher.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getRouteMatcher = getRouteMatcher;
  6. var _utils = require("../../utils");
  7. function getRouteMatcher({ re , groups }) {
  8. return (pathname)=>{
  9. const routeMatch = re.exec(pathname);
  10. if (!routeMatch) {
  11. return false;
  12. }
  13. const decode = (param)=>{
  14. try {
  15. return decodeURIComponent(param);
  16. } catch (_) {
  17. throw new _utils.DecodeError('failed to decode param');
  18. }
  19. };
  20. const params = {};
  21. Object.keys(groups).forEach((slugName)=>{
  22. const g = groups[slugName];
  23. const m = routeMatch[g.pos];
  24. if (m !== undefined) {
  25. params[slugName] = ~m.indexOf('/') ? m.split('/').map((entry)=>decode(entry)) : g.repeat ? [
  26. decode(m)
  27. ] : decode(m);
  28. }
  29. });
  30. return params;
  31. };
  32. }
  33. //# sourceMappingURL=route-matcher.js.map