navigation-kit 0.0.1
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/LICENSE +21 -0
- package/README.md +194 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +18 -0
- package/lib/types/PathMatch.d.ts +9 -0
- package/lib/types/PathMatch.js +2 -0
- package/lib/types/PathParam.d.ts +22 -0
- package/lib/types/PathParam.js +2 -0
- package/lib/types/PathPattern.d.ts +5 -0
- package/lib/types/PathPattern.js +2 -0
- package/lib/types/RouteMap.d.ts +4 -0
- package/lib/types/RouteMap.js +2 -0
- package/lib/types/RouterAdapter.d.ts +6 -0
- package/lib/types/RouterAdapter.js +2 -0
- package/lib/types/index.d.ts +5 -0
- package/lib/types/index.js +2 -0
- package/lib/utils/createRouter.d.ts +34 -0
- package/lib/utils/createRouter.js +42 -0
- package/lib/utils/generatePath.d.ts +2 -0
- package/lib/utils/generatePath.js +31 -0
- package/lib/utils/index.d.ts +3 -0
- package/lib/utils/index.js +19 -0
- package/lib/utils/matchPath.d.ts +2 -0
- package/lib/utils/matchPath.js +66 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kris Papercut
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# navigation-kit
|
|
2
|
+
|
|
3
|
+
Framework-agnostic typed routing utility for TypeScript. Wraps your route definitions into a type-safe interface for URL generation, path matching, and navigation — without depending on any specific router library.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/navigation-kit)
|
|
6
|
+
[](https://github.com/kearisp/navigation-kit/actions/workflows/publish-latest.yml)
|
|
7
|
+
[](https://github.com/kearisp/navigation-kit/blob/master/LICENSE)
|
|
8
|
+
|
|
9
|
+
[](https://www.npmjs.com/package/navigation-kit)
|
|
10
|
+
[](https://bundlephobia.com/package/navigation-kit)
|
|
11
|
+

|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install navigation-kit
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick start
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { createRouter } from "navigation-kit";
|
|
23
|
+
|
|
24
|
+
const routes = {
|
|
25
|
+
home: "/",
|
|
26
|
+
about: "/about",
|
|
27
|
+
user: "/users/:id",
|
|
28
|
+
post: "/users/:userId/posts/:postId",
|
|
29
|
+
} as const; // as const is required — see note below
|
|
30
|
+
|
|
31
|
+
const router = createRouter(routes);
|
|
32
|
+
|
|
33
|
+
router.path("user"); // "/users/:id"
|
|
34
|
+
router.url("about"); // "/about"
|
|
35
|
+
router.url("user", { id: "42" }); // "/users/42"
|
|
36
|
+
router.url("post", { userId: "1", postId: "99" }); // "/users/1/posts/99"
|
|
37
|
+
router.match("user", "/users/42"); // PathMatch | null
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
> **`as const` is required.** Without it TypeScript widens string values to `string` and loses the ability to infer route parameters. Always declare your routes object with `as const`.
|
|
41
|
+
|
|
42
|
+
## API
|
|
43
|
+
|
|
44
|
+
### `createRouter(routes, adapter?)`
|
|
45
|
+
|
|
46
|
+
Creates a typed router object from a route map.
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
const router = createRouter(routes);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
**Type parameters:**
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
createRouter<TRoutes, TNavigateOptions = void>(routes, adapter?)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
- `TRoutes` — inferred from the routes argument; use `as const` on your routes object
|
|
59
|
+
- `TNavigateOptions` — type of the `options` argument forwarded to `navigate` (e.g. `NavigateOptions` from react-router). Defaults to `void`.
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
#### `router.pattern(route)`
|
|
64
|
+
|
|
65
|
+
Returns the `PathPattern` for a named route.
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
router.pattern("user"); // { path: "/users/:id" }
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Route values can also be `PathPattern` objects directly:
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
const routes = {
|
|
75
|
+
users: { path: "/users", caseSensitive: true, end: false },
|
|
76
|
+
} as const;
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
#### `router.path(route)`
|
|
82
|
+
|
|
83
|
+
Returns the raw path string.
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
router.path("user"); // "/users/:id"
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
#### `router.url(route, params?)`
|
|
92
|
+
|
|
93
|
+
Generates a URL. TypeScript enforces that `params` is **required** for routes with dynamic segments and **forbidden** for static routes.
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
router.url("about"); // ok — no params needed
|
|
97
|
+
router.url("user", { id: "42" }); // ok
|
|
98
|
+
router.url("user"); // TS error — params required
|
|
99
|
+
router.url("about", { id: "42" }); // TS error — no params expected
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Optional segments and wildcards are supported:
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
const routes = {
|
|
106
|
+
lang: "/:lang?/articles",
|
|
107
|
+
files: "/files/*",
|
|
108
|
+
} as const;
|
|
109
|
+
|
|
110
|
+
router.url("lang", { lang: "en" }); // "/en/articles"
|
|
111
|
+
router.url("lang", { lang: null }); // "/articles"
|
|
112
|
+
router.url("files", { "*": "img/logo.png" }); // "/files/img/logo.png"
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
#### `router.match(route, pathname)`
|
|
118
|
+
|
|
119
|
+
Matches a pathname against a route pattern. Returns a `PathMatch` object or `null`.
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
const match = router.match("user", "/users/42");
|
|
123
|
+
match?.params.id; // "42"
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
#### `router.useRouter(navigate, pathname)`
|
|
129
|
+
|
|
130
|
+
Creates navigation helpers by injecting a `navigate` function and the current `pathname`. Designed to be called inside a framework hook.
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
const { to, match, path } = router.useRouter(navigate, pathname);
|
|
134
|
+
|
|
135
|
+
to("about"); // navigates to "/about"
|
|
136
|
+
to("user", { id: "42" }); // navigates to "/users/42"
|
|
137
|
+
match("user"); // matches against current pathname
|
|
138
|
+
path("user"); // "/users/:id"
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
`to()` follows the same overloads as `url()` — params are required/forbidden based on the route.
|
|
142
|
+
|
|
143
|
+
## Integration with react-router
|
|
144
|
+
|
|
145
|
+
This package has no dependency on `react-router`. Integration lives in a separate adapter package:
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
import { createRouter } from "navigation-kit";
|
|
149
|
+
import { useNavigate, useLocation, generatePath, matchPath } from "react-router";
|
|
150
|
+
import { useMemo } from "react";
|
|
151
|
+
|
|
152
|
+
export const makeRouter = <const TRoutes extends RouteMap>(routes: TRoutes) => {
|
|
153
|
+
const router = createRouter(routes, { generatePath, matchPath });
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
...router,
|
|
157
|
+
useRouter: () => {
|
|
158
|
+
const navigate = useNavigate();
|
|
159
|
+
const { pathname } = useLocation();
|
|
160
|
+
return useMemo(
|
|
161
|
+
() => router.useRouter(navigate, pathname),
|
|
162
|
+
[navigate, pathname]
|
|
163
|
+
);
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Custom adapter
|
|
170
|
+
|
|
171
|
+
Override `generatePath` and/or `matchPath` with your own implementations:
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
const router = createRouter(routes, {
|
|
175
|
+
generatePath: (path, params) => myGeneratePath(path, params),
|
|
176
|
+
matchPath: (pattern, pathname) => myMatchPath(pattern, pathname),
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Both have built-in defaults (implementations copied from react-router, no runtime dependency).
|
|
181
|
+
|
|
182
|
+
## Navigate options
|
|
183
|
+
|
|
184
|
+
Pass a second type parameter to type the options forwarded through `to()`:
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
import { NavigateOptions } from "react-router";
|
|
188
|
+
|
|
189
|
+
const router = createRouter<typeof routes, NavigateOptions>(routes);
|
|
190
|
+
|
|
191
|
+
const { to } = router.useRouter(navigate, pathname);
|
|
192
|
+
to("about", undefined, { replace: true });
|
|
193
|
+
to("user", { id: "42" }, { state: { from: "/" } });
|
|
194
|
+
```
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./types"), exports);
|
|
18
|
+
__exportStar(require("./utils"), exports);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
type _GetParam<Param extends string> = Param extends `${infer Name}?` ? {
|
|
2
|
+
[K in Name]?: string | null | undefined;
|
|
3
|
+
} : {
|
|
4
|
+
[K in Param]: string;
|
|
5
|
+
};
|
|
6
|
+
type _ParseParams<Path extends string> = Path extends `${infer _Prefix}:${infer Param}/${infer Suffix}` ? _GetParam<Param> & _ParseParams<`/${Suffix}`> : Path extends `${infer _Prefix}:${infer Param}` ? _GetParam<Param> : {};
|
|
7
|
+
export type ParseParams<Path extends string> = Path extends "*" ? {
|
|
8
|
+
"*": string;
|
|
9
|
+
} : Path extends `${infer Rest}/*` ? {
|
|
10
|
+
"*": string;
|
|
11
|
+
} & ParseParams<Rest> : _ParseParams<Path>;
|
|
12
|
+
export type Simplify<T> = {
|
|
13
|
+
[K in keyof T]: T[K];
|
|
14
|
+
} & {};
|
|
15
|
+
export type ParsedParams<Path extends string> = Simplify<ParseParams<Path> & {
|
|
16
|
+
[key in string]: string | null | undefined;
|
|
17
|
+
}>;
|
|
18
|
+
export type PathParam<Path extends string> = keyof ParseParams<Path> & string;
|
|
19
|
+
export type ParamParseKey<Segment extends string> = [
|
|
20
|
+
PathParam<Segment>
|
|
21
|
+
] extends [never] ? string : PathParam<Segment>;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { PathPattern } from "./PathPattern";
|
|
2
|
+
import { PathMatch } from "./PathMatch";
|
|
3
|
+
export interface RouterAdapter {
|
|
4
|
+
generatePath: (path: string, params?: Record<string, string | number | null | undefined>) => string;
|
|
5
|
+
matchPath: (pattern: PathPattern, pathname: string) => PathMatch | null;
|
|
6
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type { RouteMap } from "./RouteMap";
|
|
2
|
+
export type { PathPattern } from "./PathPattern";
|
|
3
|
+
export type { PathParam, ParamParseKey, ParsedParams } from "./PathParam";
|
|
4
|
+
export type { PathMatch } from "./PathMatch";
|
|
5
|
+
export type { RouterAdapter } from "./RouterAdapter";
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { RouteMap, PathPattern, PathMatch, PathParam, RouterAdapter } from "../types";
|
|
2
|
+
type RouteParams<T extends string | PathPattern> = T extends string ? {
|
|
3
|
+
[K in PathParam<T>]: string | number | null;
|
|
4
|
+
} : T extends PathPattern ? {
|
|
5
|
+
[K in PathParam<T["path"]>]: string | number | null;
|
|
6
|
+
} : never;
|
|
7
|
+
type RoutesWithParams<T extends RouteMap> = {
|
|
8
|
+
[K in keyof T]: T[K] extends string ? [PathParam<T[K]>] extends [never] ? never : K : T[K] extends PathPattern ? [PathParam<T[K]["path"]>] extends [never] ? never : K : never;
|
|
9
|
+
}[keyof T];
|
|
10
|
+
type RoutesWithoutParams<T extends RouteMap> = {
|
|
11
|
+
[K in keyof T]: T[K] extends string ? [PathParam<T[K]>] extends [never] ? K : never : T[K] extends PathPattern ? [PathParam<T[K]["path"]>] extends [never] ? K : never : never;
|
|
12
|
+
}[keyof T];
|
|
13
|
+
export declare const createRouter: <const TRoutes extends RouteMap>(routes: TRoutes, adapter?: Partial<RouterAdapter>) => {
|
|
14
|
+
pattern: <R extends keyof TRoutes>(route: R) => PathPattern;
|
|
15
|
+
path: <R extends keyof TRoutes>(route: R) => string;
|
|
16
|
+
url: {
|
|
17
|
+
<R extends RoutesWithoutParams<TRoutes>>(route: R): string;
|
|
18
|
+
<R extends RoutesWithParams<TRoutes>>(route: R, params: RouteParams<TRoutes[R]>): string;
|
|
19
|
+
};
|
|
20
|
+
match: <R extends keyof TRoutes>(route: R, pathname: string) => PathMatch | null;
|
|
21
|
+
useRouter: <TNavigateOptions = void>(navigate: (path: string, options?: TNavigateOptions) => void, pathname: string) => {
|
|
22
|
+
to: {
|
|
23
|
+
<R extends RoutesWithoutParams<TRoutes>>(route: R, params?: never, options?: TNavigateOptions): void;
|
|
24
|
+
<R extends RoutesWithParams<TRoutes>>(route: R, params: RouteParams<TRoutes[R]>, options?: TNavigateOptions): void;
|
|
25
|
+
};
|
|
26
|
+
match: <R extends keyof TRoutes>(route: R, path?: string) => PathMatch<string> | null;
|
|
27
|
+
path: <R extends keyof TRoutes>(route: R) => string;
|
|
28
|
+
url: {
|
|
29
|
+
<R extends RoutesWithoutParams<TRoutes>>(route: R): string;
|
|
30
|
+
<R extends RoutesWithParams<TRoutes>>(route: R, params: RouteParams<TRoutes[R]>): string;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createRouter = void 0;
|
|
4
|
+
const generatePath_1 = require("./generatePath");
|
|
5
|
+
const matchPath_1 = require("./matchPath");
|
|
6
|
+
const createRouter = (routes, adapter = {}) => {
|
|
7
|
+
var _a, _b;
|
|
8
|
+
const _generatePath = (_a = adapter.generatePath) !== null && _a !== void 0 ? _a : generatePath_1.generatePath;
|
|
9
|
+
const _matchPath = (_b = adapter.matchPath) !== null && _b !== void 0 ? _b : matchPath_1.matchPath;
|
|
10
|
+
const handlePattern = (route) => {
|
|
11
|
+
const pattern = routes[route];
|
|
12
|
+
return typeof pattern === "string" ? { path: pattern } : pattern;
|
|
13
|
+
};
|
|
14
|
+
const handlePath = (route) => {
|
|
15
|
+
return handlePattern(route).path;
|
|
16
|
+
};
|
|
17
|
+
function handleUrl(route, params) {
|
|
18
|
+
return _generatePath(handlePattern(route).path, params);
|
|
19
|
+
}
|
|
20
|
+
const handleMatch = (route, pathname) => {
|
|
21
|
+
return _matchPath(handlePattern(route), pathname);
|
|
22
|
+
};
|
|
23
|
+
const handleUseRouter = (navigate, pathname) => {
|
|
24
|
+
function handleTo(route, params, options) {
|
|
25
|
+
navigate(_generatePath(handlePattern(route).path, params), options);
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
to: handleTo,
|
|
29
|
+
match: (route, path = pathname) => handleMatch(route, path),
|
|
30
|
+
path: handlePath,
|
|
31
|
+
url: handleUrl
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
return {
|
|
35
|
+
pattern: handlePattern,
|
|
36
|
+
path: handlePath,
|
|
37
|
+
url: handleUrl,
|
|
38
|
+
match: handleMatch,
|
|
39
|
+
useRouter: handleUseRouter
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
exports.createRouter = createRouter;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generatePath = generatePath;
|
|
4
|
+
function generatePath(originalPath, params = {}) {
|
|
5
|
+
let path = originalPath;
|
|
6
|
+
if (path.endsWith("*") && path !== "*" && !path.endsWith("/*")) {
|
|
7
|
+
path = path.replace(/\*$/, "/*");
|
|
8
|
+
}
|
|
9
|
+
const prefix = path.startsWith("/") ? "/" : "";
|
|
10
|
+
const stringify = (p) => p == null ? "" : typeof p === "string" ? p : String(p);
|
|
11
|
+
const paramsMap = params;
|
|
12
|
+
return (prefix +
|
|
13
|
+
path.split(/\/+/)
|
|
14
|
+
.map((segment, index, array) => {
|
|
15
|
+
if (index === array.length - 1 && segment === "*") {
|
|
16
|
+
return stringify(paramsMap["*"]);
|
|
17
|
+
}
|
|
18
|
+
const keyMatch = segment.match(/^:([\w-]+)(\??)(.*)/);
|
|
19
|
+
if (keyMatch) {
|
|
20
|
+
const [, key, optional, suffix] = keyMatch;
|
|
21
|
+
const param = paramsMap[key];
|
|
22
|
+
if (optional !== "?" && param == null) {
|
|
23
|
+
throw new Error(`Missing ":${key}" param`);
|
|
24
|
+
}
|
|
25
|
+
return encodeURIComponent(stringify(param)) + suffix;
|
|
26
|
+
}
|
|
27
|
+
return segment.replace(/\?$/g, "");
|
|
28
|
+
})
|
|
29
|
+
.filter(Boolean)
|
|
30
|
+
.join("/"));
|
|
31
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./createRouter"), exports);
|
|
18
|
+
__exportStar(require("./generatePath"), exports);
|
|
19
|
+
__exportStar(require("./matchPath"), exports);
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.matchPath = matchPath;
|
|
4
|
+
function compilePath(path, caseSensitive = false, end = true) {
|
|
5
|
+
const params = [];
|
|
6
|
+
let regexpSource = "^" + path
|
|
7
|
+
.replace(/\/*\*?$/, "")
|
|
8
|
+
.replace(/^\/*/, "/")
|
|
9
|
+
.replace(/[\\.*+^${}|()[\]]/g, "\\$&")
|
|
10
|
+
.replace(/\/:([\w-]+)(\?)?/g, (match, paramName, isOptional, index, str) => {
|
|
11
|
+
params.push({ paramName, isOptional: isOptional != null });
|
|
12
|
+
if (isOptional) {
|
|
13
|
+
const nextChar = str.charAt(index + match.length);
|
|
14
|
+
if (nextChar && nextChar !== "/")
|
|
15
|
+
return "/([^\\/]*)";
|
|
16
|
+
return "(?:/([^\\/]*))?";
|
|
17
|
+
}
|
|
18
|
+
return "/([^\\/]+)";
|
|
19
|
+
})
|
|
20
|
+
.replace(/\/([\w-]+)\?(\/|$)/g, "(/$1)?$2");
|
|
21
|
+
if (path.endsWith("*")) {
|
|
22
|
+
params.push({ paramName: "*" });
|
|
23
|
+
regexpSource += path === "*" || path === "/*" ? "(.*)$" : "(?:\\/(.+)|\\/*)$";
|
|
24
|
+
}
|
|
25
|
+
else if (end) {
|
|
26
|
+
regexpSource += "\\/*$";
|
|
27
|
+
}
|
|
28
|
+
else if (path !== "" && path !== "/") {
|
|
29
|
+
regexpSource += "(?:(?=\\/|$))";
|
|
30
|
+
}
|
|
31
|
+
return [
|
|
32
|
+
!caseSensitive ? new RegExp(regexpSource, "i") : new RegExp(regexpSource),
|
|
33
|
+
params
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
function matchPath(pattern, pathname) {
|
|
37
|
+
if (typeof pattern === "string") {
|
|
38
|
+
pattern = { path: pattern, caseSensitive: false, end: true };
|
|
39
|
+
}
|
|
40
|
+
const [matcher, compiledParams] = compilePath(pattern.path, pattern.caseSensitive, pattern.end);
|
|
41
|
+
const match = pathname.match(matcher);
|
|
42
|
+
if (!match)
|
|
43
|
+
return null;
|
|
44
|
+
const matchedPathname = match[0];
|
|
45
|
+
let pathnameBase = matchedPathname.replace(/(.)\/+$/, "$1");
|
|
46
|
+
const captureGroups = match.slice(1);
|
|
47
|
+
const params = compiledParams.reduce((memo, { paramName, isOptional }, index) => {
|
|
48
|
+
if (paramName === "*") {
|
|
49
|
+
const splatValue = captureGroups[index] || "";
|
|
50
|
+
pathnameBase = matchedPathname
|
|
51
|
+
.slice(0, matchedPathname.length - splatValue.length)
|
|
52
|
+
.replace(/(.)\/+$/, "$1");
|
|
53
|
+
}
|
|
54
|
+
const value = captureGroups[index];
|
|
55
|
+
memo[paramName] = isOptional && !value
|
|
56
|
+
? undefined
|
|
57
|
+
: (value || "").replace(/%2F/g, "/");
|
|
58
|
+
return memo;
|
|
59
|
+
}, {});
|
|
60
|
+
return {
|
|
61
|
+
params: params,
|
|
62
|
+
pathname: matchedPathname,
|
|
63
|
+
pathnameBase,
|
|
64
|
+
pattern: pattern
|
|
65
|
+
};
|
|
66
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "navigation-kit",
|
|
3
|
+
"type": "commonjs",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"author": "Kris Papercut <krispcut@gmail.com>",
|
|
6
|
+
"description": "",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"main": "lib/index.js",
|
|
9
|
+
"types": "lib/index.d.ts",
|
|
10
|
+
"homepage": "https://github.com/kearisp/navigation-kit#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/kearisp/navigation-kit/issues"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/kearisp/navigation-kit.git"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"prepublishOnly": "npm run build",
|
|
20
|
+
"build": "tsc --project tsconfig.build.json",
|
|
21
|
+
"watch": "tsc -w --project tsconfig.build.json",
|
|
22
|
+
"test": "jest --colors",
|
|
23
|
+
"test-watch": "jest --colors --watchAll --coverage",
|
|
24
|
+
"make-coverage-badge": "make-coverage-badge"
|
|
25
|
+
},
|
|
26
|
+
"ts-node": {
|
|
27
|
+
"compilerOptions": {
|
|
28
|
+
"module": "CommonJS"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/jest": "^30.0.0",
|
|
33
|
+
"@types/node": "^26.0.0",
|
|
34
|
+
"jest": "^30.4.2",
|
|
35
|
+
"make-coverage-badge": "^1.2.0",
|
|
36
|
+
"ts-jest": "^29.4.11",
|
|
37
|
+
"ts-node": "^10.9.2",
|
|
38
|
+
"typescript": "^6.0.3"
|
|
39
|
+
}
|
|
40
|
+
}
|