safa-router 1.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 +587 -0
- package/package.json +43 -0
- package/src/HistoryManager.js +95 -0
- package/src/Link.js +97 -0
- package/src/MiddlewareChain.js +47 -0
- package/src/RouteMatcher.js +156 -0
- package/src/RouteTree.js +187 -0
- package/src/SafaRouter.js +464 -0
- package/src/constants.js +47 -0
- package/src/errors.js +46 -0
- package/src/index.js +16 -0
- package/src/link-helper.js +24 -0
- package/src/utils.js +122 -0
package/src/utils.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
export function normalizePath(path) {
|
|
2
|
+
if (!path || path === '/') return '/'
|
|
3
|
+
const p = path.replace(/\/+/g, '/').replace(/\/$/, '')
|
|
4
|
+
return p || '/'
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function parseQuery(search) {
|
|
8
|
+
if (!search || search === '?') return {}
|
|
9
|
+
const params = new URLSearchParams(search)
|
|
10
|
+
const result = {}
|
|
11
|
+
for (const [key, value] of params) result[key] = value
|
|
12
|
+
return result
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function joinPaths(...parts) {
|
|
16
|
+
return normalizePath(parts.filter(Boolean).join('/'))
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function isDynamicSegment(segment) {
|
|
20
|
+
return /^\[/.test(segment) && /\]$/.test(segment)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function isCatchAllSegment(segment) {
|
|
24
|
+
return /^\[\.\.\./.test(segment)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function isOptionalCatchAll(segment) {
|
|
28
|
+
return /^\[\[\.\.\./.test(segment)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function isRouteGroupSegment(segment) {
|
|
32
|
+
return /^\(/.test(segment) && /\)$/.test(segment)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function isRouteGroup(segment) {
|
|
36
|
+
return /^\(/.test(segment) && /\)$/.test(segment)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function extractParamName(segment) {
|
|
40
|
+
const m = segment.match(/^\[(?:\.\.\.)?([^\]]+)\]$/)
|
|
41
|
+
return m ? m[1] : null
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function emit(events, name, data) {
|
|
45
|
+
if (!events[name]) return
|
|
46
|
+
for (const fn of events[name]) {
|
|
47
|
+
try {
|
|
48
|
+
fn(data)
|
|
49
|
+
} catch (e) {
|
|
50
|
+
console.error(`[SafaRouter] Event handler error for "${name}":`, e)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function useRouter(router) {
|
|
56
|
+
let state = {
|
|
57
|
+
pathname: router.pathname,
|
|
58
|
+
params: router.params,
|
|
59
|
+
query: router.query,
|
|
60
|
+
loading: router.loading,
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const subscribers = new Set()
|
|
64
|
+
|
|
65
|
+
const unsubscribe = router.on('routechange', () => {
|
|
66
|
+
state = {
|
|
67
|
+
pathname: router.pathname,
|
|
68
|
+
params: router.params,
|
|
69
|
+
query: router.query,
|
|
70
|
+
loading: router.loading,
|
|
71
|
+
}
|
|
72
|
+
for (const fn of subscribers) fn(state)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
get state() { return state },
|
|
77
|
+
subscribe(fn) {
|
|
78
|
+
subscribers.add(fn)
|
|
79
|
+
fn(state)
|
|
80
|
+
return () => subscribers.delete(fn)
|
|
81
|
+
},
|
|
82
|
+
push: router.push.bind(router),
|
|
83
|
+
replace: router.replace.bind(router),
|
|
84
|
+
back: router.back.bind(router),
|
|
85
|
+
forward: router.forward.bind(router),
|
|
86
|
+
navigate: router.navigate.bind(router),
|
|
87
|
+
unsubscribe,
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function debounce(fn, delay) {
|
|
92
|
+
let timer
|
|
93
|
+
return function (...args) {
|
|
94
|
+
clearTimeout(timer)
|
|
95
|
+
timer = setTimeout(() => fn.apply(this, args), delay)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function createURL(path, base = location.origin) {
|
|
100
|
+
try {
|
|
101
|
+
return new URL(path, base)
|
|
102
|
+
} catch {
|
|
103
|
+
return null
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function isExternalURL(url) {
|
|
108
|
+
if (!url) return false
|
|
109
|
+
if (url.startsWith('//')) return true
|
|
110
|
+
if (url.startsWith('http://') || url.startsWith('https://')) {
|
|
111
|
+
try {
|
|
112
|
+
return new URL(url).origin !== location.origin
|
|
113
|
+
} catch {
|
|
114
|
+
return true
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return false
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function isSamePath(a, b) {
|
|
121
|
+
return normalizePath(a) === normalizePath(b)
|
|
122
|
+
}
|