UNPKG

3.62 kBSource Map (JSON)View Raw
1{"version":3,"file":"universal-router-generate-urls.js","sources":["../src/generateUrls.js"],"sourcesContent":["/**\n * Universal Router (https://www.kriasoft.com/universal-router/)\n *\n * Copyright © 2015-present Kriasoft, LLC. All rights reserved.\n *\n * This source code is licensed under the Apache 2.0 license found in the\n * LICENSE.txt file in the root directory of this source tree.\n */\n\n/* eslint no-param-reassign: ['error', { props: false }] */\n\nimport Router from './Router';\n\nconst cache = new Map();\n\nfunction cacheRoutes(routesByName, route, routes) {\n if (routesByName[route.name]) {\n throw new Error(`Route \"${route.name}\" already exists`);\n }\n\n if (route.name) {\n routesByName[route.name] = route;\n }\n\n if (routes) {\n for (let i = 0; i < routes.length; i += 1) {\n const childRoute = routes[i];\n childRoute.parent = route;\n cacheRoutes(routesByName, childRoute, childRoute.children);\n }\n }\n}\n\nfunction generateUrls(router, options) {\n if (!(router instanceof Router)) {\n throw new TypeError('An instance of Router is expected');\n }\n\n router.routesByName = router.routesByName || {};\n\n return (routeName, params) => {\n let route = router.routesByName[routeName];\n if (!route) {\n router.routesByName = {}; // clear cache\n cacheRoutes(router.routesByName, router.root, router.root.children);\n\n route = router.routesByName[routeName];\n if (!route) {\n throw new Error(`Route \"${routeName}\" not found`);\n }\n }\n\n let path = '';\n while (route) {\n if (route.path !== '/') {\n let toPath = cache.get(route.path);\n if (!toPath) {\n toPath = Router.pathToRegexp.compile(route.path);\n cache.set(route.path, toPath);\n }\n path = toPath(params, options) + path;\n }\n route = route.parent;\n }\n\n return router.baseUrl + path || '/';\n };\n}\n\nRouter.generateUrls = generateUrls;\n\nexport default generateUrls;\n"],"names":["cache","Map","cacheRoutes","routesByName","route","routes","name","Error","i","length","childRoute","parent","children","generateUrls","router","options","Router","TypeError","routeName","params","root","path","toPath","get","pathToRegexp","compile","set","baseUrl"],"mappings":";;;;;;;;;;AAAA;;;;;;;;;;;AAWA,AAEA,IAAMA,QAAQ,IAAIC,GAAJ,EAAd;;AAEA,SAASC,WAAT,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