router-kit 0.2.1 → 0.2.3
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.
|
@@ -6,26 +6,35 @@ import RouterContext from "./RouterContext";
|
|
|
6
6
|
const RouterProvider = ({ routes }) => {
|
|
7
7
|
const [path, setPath] = useState(window.location.pathname);
|
|
8
8
|
let fullPathWithParams = "";
|
|
9
|
+
let page404 = _jsx(Page404, {});
|
|
9
10
|
const pathValidation = (routeFullPath, currentPath) => {
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const r = routeParts[i];
|
|
16
|
-
const p = pathParts[i];
|
|
17
|
-
if (r.startsWith(":"))
|
|
11
|
+
const routePaths = routeFullPath.split("|");
|
|
12
|
+
for (const routePath of routePaths) {
|
|
13
|
+
const routeParts = routePath.split("/").filter(Boolean);
|
|
14
|
+
const pathParts = currentPath.split("/").filter(Boolean);
|
|
15
|
+
if (routeParts.length !== pathParts.length)
|
|
18
16
|
continue;
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
let isMatch = true;
|
|
18
|
+
for (let i = 0; i < routeParts.length; i++) {
|
|
19
|
+
const r = routeParts[i];
|
|
20
|
+
const p = pathParts[i];
|
|
21
|
+
if (r.startsWith(":"))
|
|
22
|
+
continue;
|
|
23
|
+
if (r !== p) {
|
|
24
|
+
isMatch = false;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (isMatch)
|
|
29
|
+
return true;
|
|
21
30
|
}
|
|
22
|
-
return
|
|
31
|
+
return false;
|
|
23
32
|
};
|
|
24
33
|
const getComponent = (routesList, currentPath, parentPath = "/") => {
|
|
25
34
|
for (const route of routesList) {
|
|
26
35
|
const is404 = route.path === "404" || route.path === "/404";
|
|
27
36
|
if (is404 && route.component) {
|
|
28
|
-
|
|
37
|
+
page404 = route.component;
|
|
29
38
|
}
|
|
30
39
|
const fullPath = join(parentPath, `/${route.path}`);
|
|
31
40
|
if (pathValidation(fullPath, currentPath)) {
|
|
@@ -42,7 +51,7 @@ const RouterProvider = ({ routes }) => {
|
|
|
42
51
|
};
|
|
43
52
|
fullPathWithParams = "";
|
|
44
53
|
const matchedComponent = getComponent(routes, path);
|
|
45
|
-
const component = matchedComponent !== null && matchedComponent !== void 0 ? matchedComponent :
|
|
54
|
+
const component = matchedComponent !== null && matchedComponent !== void 0 ? matchedComponent : page404;
|
|
46
55
|
const navigate = (to, options) => {
|
|
47
56
|
if (options && options.replace) {
|
|
48
57
|
window.history.replaceState({}, "", to);
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
function normalizeRoutes(inputRoutes) {
|
|
2
2
|
return inputRoutes.map((route) => {
|
|
3
|
-
|
|
4
|
-
const normalizedPath =
|
|
5
|
-
?
|
|
6
|
-
|
|
3
|
+
const pathArray = Array.isArray(route.path) ? route.path : [route.path];
|
|
4
|
+
const normalizedPath = pathArray
|
|
5
|
+
.map((path) => (path === null || path === void 0 ? void 0 : path.startsWith("/")) ? path.replace(/^\/+/, "") : path !== null && path !== void 0 ? path : "")
|
|
6
|
+
.join("|");
|
|
7
7
|
const normalized = {
|
|
8
8
|
...route,
|
|
9
9
|
path: normalizedPath,
|
package/package.json
CHANGED