UNPKG

3.01 kBSource Map (JSON)View Raw
1{"version":3,"file":"universal-router-generate-urls.min.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":["cacheRoutes","routesByName","route","routes","name","Error","i","length","childRoute","parent","children","generateUrls","router","options","Router","TypeError","routeName","params","root","path","toPath","cache","get","pathToRegexp","compile","set","baseUrl","Map"],"mappings":";2QAeA,SAASA,GAAYC,EAAcC,EAAOC,MACpCF,EAAaC,EAAME,WACf,IAAIC,iBAAgBH,EAAME,4BAG9BF,EAAME,SACKF,EAAME,MAAQF,GAGzBC,MACG,GAAIG,GAAI,EAAGA,EAAIH,EAAOI,OAAQD,GAAK,EAAG,IACnCE,GAAaL,EAAOG,KACfG,OAASP,IACRD,EAAcO,EAAYA,EAAWE,WAKvD,QAASC,GAAaC,EAAQC,QACtBD,YAAkBE,SAChB,IAAIC,WAAU,8CAGfd,aAAeW,EAAOX,iBAEtB,SAACe,EAAWC,MACbf,GAAQU,EAAOX,aAAae,QAC3Bd,MACID,kBACKW,EAAOX,aAAcW,EAAOM,KAAMN,EAAOM,KAAKR,YAElDE,EAAOX,aAAae,UAEpB,IAAIX,iBAAgBW,wBAI1BG,GAAO,GACJjB,GAAO,IACO,MAAfA,EAAMiB,KAAc,IAClBC,GAASC,EAAMC,IAAIpB,EAAMiB,KACxBC,OACMN,EAAOS,aAAaC,QAAQtB,EAAMiB,QACrCM,IAAIvB,EAAMiB,KAAMC,MAEjBA,EAAOH,EAAQJ,GAAWM,IAE3BjB,EAAMO,aAGTG,GAAOc,QAAUP,GAAQ,gCAtDpC,IAEME,GAAQ,GAAIM,WAwDlBb,GAAOH,aAAeA"}
\No newline at end of file