stateshape 0.2.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.
- package/LICENSE +21 -0
- package/README.md +166 -0
- package/dist/index.cjs +517 -0
- package/dist/index.d.ts +268 -0
- package/dist/index.mjs +506 -0
- package/index.ts +25 -0
- package/package.json +42 -0
- package/src/EventEmitter.ts +72 -0
- package/src/PersistentState.ts +99 -0
- package/src/Route.ts +223 -0
- package/src/State.ts +101 -0
- package/src/URLState.ts +143 -0
- package/src/isState.ts +20 -0
- package/src/types/EventCallback.ts +1 -0
- package/src/types/EventCallbackMap.ts +5 -0
- package/src/types/LinkElement.ts +1 -0
- package/src/types/LocationObject.ts +11 -0
- package/src/types/LocationPattern.ts +6 -0
- package/src/types/LocationValue.ts +6 -0
- package/src/types/MatchHandler.ts +6 -0
- package/src/types/MatchState.ts +22 -0
- package/src/types/NavigationOptions.ts +10 -0
- package/src/types/PersistentStorage.ts +4 -0
- package/src/types/URLComponents.ts +4 -0
- package/src/types/URLConfig.ts +5 -0
- package/src/types/URLData.ts +10 -0
- package/src/types/URLSchema.ts +4 -0
- package/src/utils/compileURL.ts +27 -0
- package/src/utils/getNavigationOptions.ts +15 -0
- package/src/utils/isLocationObject.ts +11 -0
- package/src/utils/isRouteEvent.ts +11 -0
- package/src/utils/matchURL.ts +99 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
import type { LocationValue } from "./LocationValue.ts";
|
|
3
|
+
import type { URLComponents } from "./URLComponents.ts";
|
|
4
|
+
import type { URLSchema } from "./URLSchema.ts";
|
|
5
|
+
|
|
6
|
+
export type URLData<T extends LocationValue = LocationValue> = T extends {
|
|
7
|
+
_schema: URLSchema;
|
|
8
|
+
}
|
|
9
|
+
? StandardSchemaV1.InferOutput<T["_schema"]>
|
|
10
|
+
: URLComponents;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { QuasiURL } from "quasiurl";
|
|
2
|
+
import type { LocationValue } from "../types/LocationValue.ts";
|
|
3
|
+
import type { URLData } from "../types/URLData.ts";
|
|
4
|
+
import { isLocationObject } from "./isLocationObject.ts";
|
|
5
|
+
|
|
6
|
+
export function compileURL<T extends LocationValue>(
|
|
7
|
+
urlPattern: T,
|
|
8
|
+
data?: URLData<T>,
|
|
9
|
+
) {
|
|
10
|
+
if (isLocationObject(urlPattern)) return urlPattern.compile(data);
|
|
11
|
+
|
|
12
|
+
let url = new QuasiURL(urlPattern ?? "");
|
|
13
|
+
let query = data?.query;
|
|
14
|
+
|
|
15
|
+
if (query) {
|
|
16
|
+
url.search = new URLSearchParams(
|
|
17
|
+
// Remove null und undefined values, stringify nonstring values
|
|
18
|
+
Object.entries(query).reduce<Record<string, string>>((p, [k, v]) => {
|
|
19
|
+
if (v !== null && v !== undefined)
|
|
20
|
+
p[k] = typeof v === "string" ? v : JSON.stringify(v);
|
|
21
|
+
return p;
|
|
22
|
+
}, {}),
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return url.href;
|
|
27
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { LinkElement } from "../types/LinkElement.ts";
|
|
2
|
+
import type { NavigationOptions } from "../types/NavigationOptions.ts";
|
|
3
|
+
|
|
4
|
+
export function getNavigationOptions(element: LinkElement) {
|
|
5
|
+
let { id, spa, history, scroll } = element.dataset;
|
|
6
|
+
|
|
7
|
+
return {
|
|
8
|
+
href: element.getAttribute("href"),
|
|
9
|
+
target: element.getAttribute("target"),
|
|
10
|
+
spa,
|
|
11
|
+
history,
|
|
12
|
+
scroll,
|
|
13
|
+
id,
|
|
14
|
+
} as NavigationOptions;
|
|
15
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function isRouteEvent(event: unknown) {
|
|
2
|
+
return (
|
|
3
|
+
event !== null &&
|
|
4
|
+
typeof event === "object" &&
|
|
5
|
+
(!("button" in event) || event.button === 0) &&
|
|
6
|
+
(!("ctrlKey" in event) || !event.ctrlKey) &&
|
|
7
|
+
(!("shiftKey" in event) || !event.shiftKey) &&
|
|
8
|
+
(!("altKey" in event) || !event.altKey) &&
|
|
9
|
+
(!("metaKey" in event) || !event.metaKey)
|
|
10
|
+
);
|
|
11
|
+
}
|
|
@@ -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,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"include": ["./index.ts", "./playwright.config.ts", "./tests", "./src"],
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"declaration": true,
|
|
5
|
+
"emitDeclarationOnly": true,
|
|
6
|
+
"lib": ["ESNext", "DOM"],
|
|
7
|
+
"target": "esnext",
|
|
8
|
+
"outDir": "dist",
|
|
9
|
+
"module": "nodenext",
|
|
10
|
+
"moduleResolution": "nodenext",
|
|
11
|
+
"allowImportingTsExtensions": true,
|
|
12
|
+
"strict": true,
|
|
13
|
+
"noUnusedLocals": true,
|
|
14
|
+
"noUnusedParameters": true
|
|
15
|
+
}
|
|
16
|
+
}
|