router-kit 0.1.5 → 0.1.6

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.
@@ -1,48 +1,49 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useEffect, useState } from "react";
3
3
  import join from "url-join";
4
+ import { routes } from "../core/createRouter";
4
5
  import Page404 from "../pages/404";
5
6
  import RouterContext from "./RouterContext";
6
- import { router } from "../core/createRouter";
7
7
  const RouterProvider = () => {
8
8
  const [path, setPath] = useState(window.location.pathname);
9
9
  let fullPathWithParams = "";
10
- const pathValidation = (fullPath, path) => {
11
- const fakePathArr = path.split("/");
12
- let fakePath = "/";
13
- fullPath.split("/").forEach((e, index) => {
14
- if (e.startsWith(":")) {
15
- fakePathArr[index] = e;
16
- }
17
- });
18
- fakePathArr.forEach((e) => {
19
- fakePath = join(fakePath, `/${e}`);
20
- });
21
- if (fullPath === fakePath) {
22
- fakePath = "";
23
- return true;
10
+ const pathValidation = (routeFullPath, currentPath) => {
11
+ const routeParts = routeFullPath.split("/").filter(Boolean);
12
+ const pathParts = currentPath.split("/").filter(Boolean);
13
+ if (routeParts.length !== pathParts.length)
14
+ return false;
15
+ for (let i = 0; i < routeParts.length; i++) {
16
+ const r = routeParts[i];
17
+ const p = pathParts[i];
18
+ if (r.startsWith(":"))
19
+ continue;
20
+ if (r !== p)
21
+ return false;
24
22
  }
25
- fakePath = "";
26
- return false;
23
+ return true;
27
24
  };
28
- const getComponent = (routes, currentPath, parentPath = "/") => {
29
- let component = _jsx(Page404, {});
30
- for (const route of routes) {
31
- if (route.path === "/404" && route.component)
32
- component = route.component;
25
+ const getComponent = (routesList, currentPath, parentPath = "/") => {
26
+ for (const route of routesList) {
27
+ const is404 = route.path === "404" || route.path === "/404";
28
+ if (is404 && route.component) {
29
+ // don't return here; keep as fallback at top-level
30
+ }
33
31
  const fullPath = join(parentPath, `/${route.path}`);
34
32
  if (pathValidation(fullPath, currentPath)) {
35
33
  fullPathWithParams = fullPath;
36
- component = route.component;
37
- break;
34
+ return route.component;
38
35
  }
39
36
  if (route.children) {
40
- component = getComponent(route.children, currentPath, fullPath);
37
+ const childMatch = getComponent(route.children, currentPath, fullPath);
38
+ if (childMatch)
39
+ return childMatch;
41
40
  }
42
41
  }
43
- return component;
42
+ return null;
44
43
  };
45
- const component = getComponent(router, path);
44
+ fullPathWithParams = "";
45
+ const matchedComponent = getComponent(routes, path);
46
+ const component = matchedComponent !== null && matchedComponent !== void 0 ? matchedComponent : _jsx(Page404, {});
46
47
  const navigate = (to, options) => {
47
48
  if (options && options.replace) {
48
49
  window.history.replaceState({}, "", to);
@@ -1,4 +1,4 @@
1
1
  import type { Route } from "../types";
2
- export declare let router: Route[];
3
- declare function createRouter(routes: Route[]): void;
2
+ export declare let routes: Route[];
3
+ declare function createRouter(inputRoutes: Route[]): Route[];
4
4
  export default createRouter;
@@ -1,6 +1,6 @@
1
- export let router = [];
2
- function normalizeRoutes(routes) {
3
- const normalizedRoutes = routes.map((route) => {
1
+ export let routes = [];
2
+ function normalizeRoutes(inputRoutes) {
3
+ const normalizedRoutes = inputRoutes.map((route) => {
4
4
  var _a, _b;
5
5
  const normalizedPath = ((_a = route.path) === null || _a === void 0 ? void 0 : _a.startsWith("/"))
6
6
  ? route.path.replace(/^\/+/, "")
@@ -14,10 +14,10 @@ function normalizeRoutes(routes) {
14
14
  }
15
15
  return normalized;
16
16
  });
17
- router = normalizedRoutes;
17
+ routes = normalizedRoutes;
18
18
  return normalizedRoutes;
19
19
  }
20
- function createRouter(routes) {
21
- normalizeRoutes(routes);
20
+ function createRouter(inputRoutes) {
21
+ return (routes = normalizeRoutes(inputRoutes));
22
22
  }
23
23
  export default createRouter;
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "email": "mohammed.bencheikh.dev@gmail.com",
6
6
  "url": "https://mohammedbencheikh.com/"
7
7
  },
8
- "version": "0.1.5",
8
+ "version": "0.1.6",
9
9
  "description": "A small React routing provider library",
10
10
  "main": "dist/index.js",
11
11
  "types": "dist/index.d.ts",
@@ -42,5 +42,13 @@
42
42
  "route-provider",
43
43
  "route-kit"
44
44
  ],
45
+ "repository": {
46
+ "type": "git",
47
+ "url": "https://github.com/Mohammed-Ben-Cheikh/router-kit.git"
48
+ },
49
+ "bugs": {
50
+ "url": "https://github.com/Mohammed-Ben-Cheikh/router-kit/issues"
51
+ },
52
+ "homepage": "https://github.com/Mohammed-Ben-Cheikh/router-kit#readme",
45
53
  "license": "MIT"
46
54
  }