{"version":3,"file":"browser.mjs","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 UniversalRouter from './UniversalRouter';\n\nconst { pathToRegexp } = UniversalRouter;\nconst cache = new Map();\n\nfunction cacheRoutes(routesByName, route, routes) {\n  if (routesByName.has(route.name)) {\n    throw new Error(`Route \"${route.name}\" already exists`);\n  }\n\n  if (route.name) {\n    routesByName.set(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 UniversalRouter)) {\n    throw new TypeError('An instance of UniversalRouter is expected');\n  }\n\n  router.routesByName = router.routesByName || new Map();\n\n  return (routeName, params) => {\n    let route = router.routesByName.get(routeName);\n    if (!route) {\n      router.routesByName.clear(); // clear cache\n      cacheRoutes(router.routesByName, router.root, router.root.children);\n\n      route = router.routesByName.get(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        const path = Array.isArray(rt.path) ? rt.path[0] : rt.path;\n        if (path) {\n          fullPath = path + fullPath;\n        }\n        rt = rt.parent;\n      }\n      const tokens = pathToRegexp.parse(fullPath);\n      const toPath = 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 = {};\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\nUniversalRouter.generateUrls = generateUrls;\n\nexport default generateUrls;\n"],"names":["pathToRegexp","UniversalRouter","cache","Map","cacheRoutes","routesByName","route","routes","has","name","Error","set","i","length","childRoute","parent","children","generateUrls","router","options","TypeError","routeName","params","get","clear","root","regexp","fullPath","rt","path","Array","isArray","tokens","parse","toPath","tokensToFunction","keys","Object","create","url","baseUrl","stringifyQueryParams","queryParams","key","query","charAt"],"mappings":";;;;AAAA;;;;;;;;;;;AAWA,IAEQA,eAAiBC,gBAAjBD;;AACR,IAAME,QAAQ,IAAIC,GAAJ,EAAd;;AAEA,SAASC,WAAT,CAAqBC,YAArB,EAAmCC,KAAnC,EAA0CC,MAA1C,EAAkD;MAC5CF,aAAaG,GAAb,CAAiBF,MAAMG,IAAvB,CAAJ,EAAkC;UAC1B,IAAIC,KAAJ,aAAoBJ,MAAMG,IAA1B,sBAAN;;;MAGEH,MAAMG,IAAV,EAAgB;iBACDE,GAAb,CAAiBL,MAAMG,IAAvB,EAA6BH,KAA7B;;;MAGEC,MAAJ,EAAY;SACL,IAAIK,IAAI,CAAb,EAAgBA,IAAIL,OAAOM,MAA3B,EAAmCD,KAAK,CAAxC,EAA2C;UACnCE,aAAaP,OAAOK,CAAP,CAAnB;iBACWG,MAAX,GAAoBT,KAApB;kBACYD,YAAZ,EAA0BS,UAA1B,EAAsCA,WAAWE,QAAjD;;;;;AAKN,SAASC,YAAT,CAAsBC,MAAtB,EAA4C;MAAdC,OAAc,uEAAJ,EAAI;;MACtC,EAAED,kBAAkBjB,e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