UNPKG

4.49 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 regexp = cache.get(route.fullPath);\n if (!regexp) {\n let fullPath = '';\n let rt = route;\n while (rt) {\n if (rt.path !== '/') {\n fullPath = rt.path + fullPath;\n }\n rt = rt.parent;\n }\n const tokens = Router.pathToRegexp.parse(fullPath);\n const toPath = Router.pathToRegexp.tokensToFunction(tokens);\n const keys = Object.create(null);\n for (let i = 0; i < tokens.length; i += 1) {\n if (typeof tokens[i] !== 'string') {\n keys[tokens[i].name] = true;\n }\n }\n regexp = { toPath, keys };\n cache.set(fullPath, regexp);\n route.fullPath = fullPath;\n }\n\n let url = router.baseUrl + regexp.toPath(params, options) || '/';\n\n if (options.stringifyQueryParams && params) {\n const queryParams = Object.create(null);\n const keys = Object.keys(params);\n for (let i = 0; i < keys.length; i += 1) {\n const key = keys[i];\n if (!regexp.keys[key]) {\n queryParams[key] = params[key];\n }\n }\n const query = options.stringifyQueryParams(queryParams);\n if (query) {\n url += query.charAt(0) === '?' ? query : `?${query}`;\n }\n }\n\n return url;\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","regexp","cache","get","fullPath","rt","path","tokens","pathToRegexp","parse","toPath","tokensToFunction","keys","Object","create","set","url","baseUrl","stringifyQueryParams","queryParams","key","query","charAt","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,MAAQC,kEACtBD,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,oBAI1BG,GAASC,EAAMC,IAAInB,EAAMoB,cACxBH,EAAQ,QACPG,GAAW,GACXC,EAAKrB,EACFqB,GACW,MAAZA,EAAGC,SACMD,EAAGC,KAAOF,KAElBC,EAAGd,WAKL,GAHCgB,GAASX,EAAOY,aAAaC,MAAML,GACnCM,EAASd,EAAOY,aAAaG,iBAAiBJ,GAC9CK,EAAOC,OAAOC,OAAO,MAClB1B,EAAI,EAAGA,EAAImB,EAAOlB,OAAQD,GAAK,EACb,gBAAdmB,GAAOnB,OACXm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