@uxf/router 13.0.0 → 14.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/create-router.js +7 -5
- package/create-router.test.js +5 -0
- package/package.json +4 -4
package/create-router.js
CHANGED
|
@@ -52,13 +52,15 @@ function createRouter(routes, routerOptions) {
|
|
|
52
52
|
throw new Error(`Route '${String(route)}' not found.`);
|
|
53
53
|
}
|
|
54
54
|
const restParams = {};
|
|
55
|
+
// Values go in through a replacer function: as a plain replacement string, `$&`, `$$` and the like in a
|
|
56
|
+
// value would be expanded instead of kept.
|
|
55
57
|
Object.keys(params !== null && params !== void 0 ? params : empty_object_1.EMPTY_OBJECT).forEach((key) => {
|
|
56
58
|
const value = params[key];
|
|
57
59
|
const segment = `[${key}]`;
|
|
58
60
|
const catchAllSegments = `[...${key}]`;
|
|
59
61
|
const catchAllSegmentsOptional = `[[...${key}]]`;
|
|
60
62
|
if (pathname.includes(segment)) {
|
|
61
|
-
pathname = pathname.replace(segment, value);
|
|
63
|
+
pathname = pathname.replace(segment, () => String(value));
|
|
62
64
|
}
|
|
63
65
|
else if (pathname.includes(catchAllSegmentsOptional)) {
|
|
64
66
|
if ((Array.isArray(value) && (0, is_empty_1.isEmpty)(value)) || value === null || value === undefined || value === "") {
|
|
@@ -66,10 +68,10 @@ function createRouter(routes, routerOptions) {
|
|
|
66
68
|
}
|
|
67
69
|
else if (Array.isArray(value)) {
|
|
68
70
|
// catch all segments optional
|
|
69
|
-
pathname = pathname.replace(catchAllSegmentsOptional, value.join("/"));
|
|
71
|
+
pathname = pathname.replace(catchAllSegmentsOptional, () => value.join("/"));
|
|
70
72
|
}
|
|
71
73
|
else {
|
|
72
|
-
pathname = pathname.replace(catchAllSegmentsOptional, value);
|
|
74
|
+
pathname = pathname.replace(catchAllSegmentsOptional, () => String(value));
|
|
73
75
|
}
|
|
74
76
|
}
|
|
75
77
|
else if (pathname.includes(catchAllSegments)) {
|
|
@@ -78,10 +80,10 @@ function createRouter(routes, routerOptions) {
|
|
|
78
80
|
if ((0, is_empty_1.isEmpty)(value)) {
|
|
79
81
|
throw new Error(`Parameter '${key}' can not be empty array for route '${String(route)}'`);
|
|
80
82
|
}
|
|
81
|
-
pathname = pathname.replace(catchAllSegments, value.join("/"));
|
|
83
|
+
pathname = pathname.replace(catchAllSegments, () => value.join("/"));
|
|
82
84
|
}
|
|
83
85
|
else {
|
|
84
|
-
pathname = pathname.replace(catchAllSegments, value);
|
|
86
|
+
pathname = pathname.replace(catchAllSegments, () => String(value));
|
|
85
87
|
}
|
|
86
88
|
}
|
|
87
89
|
else {
|
package/create-router.test.js
CHANGED
|
@@ -157,6 +157,11 @@ test("routeToUrl throws when a required param is missing", () => {
|
|
|
157
157
|
// @ts-expect-error pathParams is required, so this call is invalid on purpose — the point is the runtime guard
|
|
158
158
|
routeToUrl("catchAllSegments", {})).toThrow("Missing parameter '[...pathParams]' for route 'catchAllSegments'.");
|
|
159
159
|
});
|
|
160
|
+
test('routeToUrl puts "$" in a param value literally', () => {
|
|
161
|
+
expect(routeToUrl("manyParameters", { param1: "a$&b", param2: "$$" })).toBe("/many-parameters/a$&b/form/$$");
|
|
162
|
+
expect(routeToUrl("catchAllSegments", { pathParams: ["x$`y"] })).toBe("/catch-all/x$`y");
|
|
163
|
+
expect(routeToUrl("optionalCatchAllSegments", { pathParams: ["$'"] })).toBe("/catch-all-optional/$'");
|
|
164
|
+
});
|
|
160
165
|
test("create route matcher", () => {
|
|
161
166
|
expect(createRouteMatcher("manyParameters", { param1: "value-1" })("/many-parameters/value-1/form/any", {
|
|
162
167
|
param1: "value-1",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uxf/router",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "14.0.0",
|
|
4
4
|
"description": "UXF Router",
|
|
5
5
|
"author": "UXFans <dev@uxf.cz>",
|
|
6
6
|
"homepage": "https://gitlab.com/uxf-npm/router#readme",
|
|
@@ -26,12 +26,12 @@
|
|
|
26
26
|
"true-case-path": "2.2.1"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
|
-
"@uxf/core": "
|
|
30
|
-
"next": ">=
|
|
29
|
+
"@uxf/core": "14.0.0",
|
|
30
|
+
"next": ">=16.0.0",
|
|
31
31
|
"superstruct": "^2.0.2"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@uxf/core": "
|
|
34
|
+
"@uxf/core": "14.0.0",
|
|
35
35
|
"next": "16.3.8",
|
|
36
36
|
"superstruct": "^2.0.2"
|
|
37
37
|
}
|