UNPKG

1.39 kBJavaScriptView Raw
1/**
2 * react-router v8.0.0
3 *
4 * Copyright (c) Remix Software Inc.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE.md file in the root directory of this source tree.
8 *
9 * @license MIT
10 */
11//#region lib/href.ts
12/**
13Returns a resolved URL path for the specified route.
14
15```tsx
16const h = href("/:lang?/about", { lang: "en" })
17// -> `/en/about`
18
19<Link to={href("/products/:id", { id: "abc123" })} />
20```
21*/
22function href(path, ...args) {
23 let params = args[0];
24 let result = trimTrailingSplat(path).replace(/\/:([\w-]+)(\?)?/g, (_, param, questionMark) => {
25 const isRequired = questionMark === void 0;
26 const value = params?.[param];
27 if (isRequired && value === void 0) throw new Error(`Path '${path}' requires param '${param}' but it was not provided`);
28 return value === void 0 ? "" : "/" + value;
29 });
30 if (path.endsWith("*")) {
31 const value = params?.["*"];
32 if (value !== void 0) result += "/" + value;
33 }
34 return result || "/";
35}
36/**
37* Removes a trailing splat and any number of slashes from the end of the path.
38*
39* Benchmarked to be faster than `path.replace(/\/*\*?$/, "")`, which backtracks.
40*/
41function trimTrailingSplat(path) {
42 let i = path.length - 1;
43 let char = path[i];
44 if (char !== "*" && char !== "/") return path;
45 i--;
46 for (; i >= 0; i--) if (path[i] !== "/") break;
47 return path.slice(0, i + 1);
48}
49//#endregion
50export { href };