compact-router 0.0.0-stage → 0.1.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.
@@ -0,0 +1,99 @@
1
+ import type { StandardSchemaV1 } from "@standard-schema/spec";
2
+ import { QuasiURL } from "quasiurl";
3
+ import type { LocationPattern } from "../types/LocationPattern.ts";
4
+ import type { MatchState } from "../types/MatchState.ts";
5
+ import type { URLComponents } from "../types/URLComponents.ts";
6
+ import type { URLSchema } from "../types/URLSchema.ts";
7
+ import { isLocationObject } from "./isLocationObject.ts";
8
+
9
+ function toObject(x: string[]) {
10
+ return x.reduce<Record<string, string>>((p, v, k) => {
11
+ p[String(k)] = v;
12
+
13
+ return p;
14
+ }, {});
15
+ }
16
+
17
+ function matchPattern<P extends LocationPattern>(pattern: P, href: string) {
18
+ let query = Object.fromEntries(
19
+ new URLSearchParams(new QuasiURL(href).search),
20
+ );
21
+
22
+ if (typeof pattern === "string")
23
+ return {
24
+ ok: pattern === "*" || pattern === href,
25
+ href,
26
+ params: {},
27
+ query,
28
+ };
29
+
30
+ if (pattern instanceof RegExp) {
31
+ let matches = pattern.exec(href);
32
+
33
+ return {
34
+ ok: matches !== null,
35
+ href,
36
+ params: matches
37
+ ? {
38
+ ...toObject(Array.from(matches).slice(1)),
39
+ ...matches.groups,
40
+ }
41
+ : {},
42
+ query,
43
+ };
44
+ }
45
+
46
+ if (isLocationObject(pattern)) {
47
+ let result = pattern.exec(href) as
48
+ | (P extends {
49
+ _schema: URLSchema;
50
+ }
51
+ ? StandardSchemaV1.InferOutput<P["_schema"]>
52
+ : URLComponents)
53
+ | null;
54
+
55
+ if (result === null)
56
+ return {
57
+ ok: false,
58
+ href,
59
+ params: {},
60
+ query: {},
61
+ };
62
+
63
+ return {
64
+ ok: true,
65
+ href,
66
+ params: result.params ?? {},
67
+ query: result.query ?? {},
68
+ };
69
+ }
70
+
71
+ return {
72
+ ok: false,
73
+ href,
74
+ params: {},
75
+ query: {},
76
+ };
77
+ }
78
+
79
+ export function matchURL<P extends LocationPattern>(
80
+ pattern: P,
81
+ href: string,
82
+ ): MatchState<P> {
83
+ if (Array.isArray(pattern)) {
84
+ for (let p of pattern) {
85
+ let result = matchPattern(p, href);
86
+
87
+ if (result.ok) return result as MatchState<P>;
88
+ }
89
+
90
+ return {
91
+ ok: false,
92
+ href,
93
+ params: {},
94
+ query: {},
95
+ } as MatchState<P>;
96
+ }
97
+
98
+ return matchPattern(pattern, href) as MatchState<P>;
99
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "include": ["./index.ts", "./playwright.config.ts", "./tests", "./src"],
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "emitDeclarationOnly": true,
6
+ "lib": ["ESNext", "DOM", "DOM.Iterable"],
7
+ "types": ["node"],
8
+ "target": "esnext",
9
+ "outDir": "dist",
10
+ "module": "nodenext",
11
+ "moduleResolution": "nodenext",
12
+ "allowImportingTsExtensions": true,
13
+ "strict": true,
14
+ "noUnusedLocals": true,
15
+ "noUnusedParameters": true
16
+ }
17
+ }