router-kit 0.1.4 → 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,5 +1,2 @@
1
- import type { Route } from "../types";
2
- declare const RouterProvider: ({ routes }: {
3
- routes: Route[];
4
- }) => import("react/jsx-runtime").JSX.Element;
1
+ declare const RouterProvider: () => import("react/jsx-runtime").JSX.Element;
5
2
  export default RouterProvider;
@@ -1,47 +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
- const RouterProvider = ({ routes }) => {
7
+ const RouterProvider = () => {
7
8
  const [path, setPath] = useState(window.location.pathname);
8
9
  let fullPathWithParams = "";
9
- const pathValidation = (fullPath, path) => {
10
- const fakePathArr = path.split("/");
11
- let fakePath = "/";
12
- fullPath.split("/").forEach((e, index) => {
13
- if (e.startsWith(":")) {
14
- fakePathArr[index] = e;
15
- }
16
- });
17
- fakePathArr.forEach((e) => {
18
- fakePath = join(fakePath, `/${e}`);
19
- });
20
- if (fullPath === fakePath) {
21
- fakePath = "";
22
- 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;
23
22
  }
24
- fakePath = "";
25
- return false;
23
+ return true;
26
24
  };
27
- const getComponent = (routes, currentPath, parentPath = "/") => {
28
- let component = _jsx(Page404, {});
29
- for (const route of routes) {
30
- if (route.path === "/404" && route.component)
31
- 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
+ }
32
31
  const fullPath = join(parentPath, `/${route.path}`);
33
32
  if (pathValidation(fullPath, currentPath)) {
34
33
  fullPathWithParams = fullPath;
35
- component = route.component;
36
- break;
34
+ return route.component;
37
35
  }
38
36
  if (route.children) {
39
- component = getComponent(route.children, currentPath, fullPath);
37
+ const childMatch = getComponent(route.children, currentPath, fullPath);
38
+ if (childMatch)
39
+ return childMatch;
40
40
  }
41
41
  }
42
- return component;
42
+ return null;
43
43
  };
44
- const component = getComponent(routes, path);
44
+ fullPathWithParams = "";
45
+ const matchedComponent = getComponent(routes, path);
46
+ const component = matchedComponent !== null && matchedComponent !== void 0 ? matchedComponent : _jsx(Page404, {});
45
47
  const navigate = (to, options) => {
46
48
  if (options && options.replace) {
47
49
  window.history.replaceState({}, "", to);
@@ -1,3 +1,4 @@
1
1
  import type { Route } from "../types";
2
- declare function createRouter(routes: Route[]): Route[];
2
+ export declare let routes: Route[];
3
+ declare function createRouter(inputRoutes: Route[]): Route[];
3
4
  export default createRouter;
@@ -1,9 +1,10 @@
1
- // Normalize routes: remove leading slashes from paths and normalize children recursively
2
- function normalizeRoutes(routes) {
3
- return routes.map((route) => {
4
- const normalizedPath = route.path.startsWith("/")
1
+ export let routes = [];
2
+ function normalizeRoutes(inputRoutes) {
3
+ const normalizedRoutes = inputRoutes.map((route) => {
4
+ var _a, _b;
5
+ const normalizedPath = ((_a = route.path) === null || _a === void 0 ? void 0 : _a.startsWith("/"))
5
6
  ? route.path.replace(/^\/+/, "")
6
- : route.path;
7
+ : (_b = route.path) !== null && _b !== void 0 ? _b : "";
7
8
  const normalized = {
8
9
  ...route,
9
10
  path: normalizedPath,
@@ -13,8 +14,10 @@ function normalizeRoutes(routes) {
13
14
  }
14
15
  return normalized;
15
16
  });
17
+ routes = normalizedRoutes;
18
+ return normalizedRoutes;
16
19
  }
17
- function createRouter(routes) {
18
- return normalizeRoutes(routes);
20
+ function createRouter(inputRoutes) {
21
+ return (routes = normalizeRoutes(inputRoutes));
19
22
  }
20
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.4",
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",
@@ -37,8 +37,18 @@
37
37
  "react",
38
38
  "route",
39
39
  "provider",
40
+ "kit",
40
41
  "routing",
41
- "route-provider"
42
+ "route-provider",
43
+ "route-kit"
42
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",
43
53
  "license": "MIT"
44
54
  }