UNPKG

@bravissimolabs/react-router-config

Version:

Static route config matching for React Router

29 lines (20 loc) 804 B
import matchPath from "react-router/matchPath"; import Router from "react-router/Router"; // ensure we're using the exact code for default root match var computeMatch = Router.prototype.computeMatch; var matchRoutes = function matchRoutes(routes, pathname) { var branch = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; routes.some(function (route) { var match = route.path ? matchPath(pathname, route) : branch.length ? branch[branch.length - 1].match // use parent match : computeMatch(pathname); // use default "root" match if (match) { branch.push({ route: route, match: match }); if (route.routes) { matchRoutes(route.routes, pathname, branch); } } return match; }); return branch; }; export default matchRoutes;