UNPKG

universal-router

Version:

Isomorphic router for JavaScript web applications

108 lines (107 loc) 4.08 kB
"use strict"; /** * Universal Router (https://www.kriasoft.com/universal-router/) * * Copyright (c) 2015-present Kriasoft. * * This source code is licensed under the MIT license found in the * LICENSE.txt file in the root directory of this source tree. */ var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var path_to_regexp_js_1 = require("./path-to-regexp.js"); function cacheRoutes(routesByName, route, routes, name, sep) { if (route.name && name && routesByName.has(name)) { throw new Error("Route \"".concat(name, "\" already exists")); } if (route.name && name) { routesByName.set(name, route); } if (routes) { for (var i = 0; i < routes.length; i++) { var childRoute = routes[i]; var childName = childRoute.name; childRoute.parent = route; cacheRoutes(routesByName, childRoute, childRoute.children, name && sep ? (childName ? name + sep + childName : name) : childName, sep); } } } /** * Create a function to generate urls by route names. */ function generateUrls(router, options) { if (!router) { throw new ReferenceError('Router is not defined'); } var routesByName = new Map(); var regexpByRoute = new Map(); var opts = __assign({ encode: encodeURIComponent }, options); return function (routeName, params) { var route = routesByName.get(routeName); if (!route) { routesByName.clear(); regexpByRoute.clear(); cacheRoutes(routesByName, router.root, router.root.children, router.root.name, opts.uniqueRouteNameSep); route = routesByName.get(routeName); if (!route) { throw new Error("Route \"".concat(routeName, "\" not found")); } } var regexp = regexpByRoute.get(route); if (!regexp) { var fullPath = ''; var rt = route; while (rt) { var path = Array.isArray(rt.path) ? rt.path[0] : rt.path; if (path) { fullPath = (path instanceof path_to_regexp_js_1.TokenData ? (0, path_to_regexp_js_1.stringify)(path) : path) + fullPath; } rt = rt.parent; } var tokens = (0, path_to_regexp_js_1.parse)(fullPath, opts); var toPath = (0, path_to_regexp_js_1.compile)(fullPath, opts); var keys = Object.create(null); for (var i = 0; i < tokens.tokens.length; i++) { var token = tokens.tokens[i]; if (token && token.type !== 'text') { if (token.type === 'group') { keys[String(i)] = true; } else { keys[token.name] = true; } } } regexp = { toPath: toPath, keys: keys }; regexpByRoute.set(route, regexp); } var url = router.baseUrl + regexp.toPath(params) || '/'; if (opts.stringifyQueryParams && params) { var queryParams = {}; var keys = Object.keys(params); for (var i = 0; i < keys.length; i++) { var key = keys[i]; if (key && !regexp.keys[key] && params[key] != null) { queryParams[key] = params[key]; } } var query = opts.stringifyQueryParams(queryParams); if (query) { url += query.charAt(0) === '?' ? query : "?".concat(query); } } return url; }; } exports.default = generateUrls;