-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShared.ts
More file actions
102 lines (98 loc) · 3.15 KB
/
Shared.ts
File metadata and controls
102 lines (98 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import type { RouterMethodData, RouterNode, RouterParamsMap } from '@app/Types.ts'
/**
* Extract route parameters from path segments using parameter mapping.
* @param segments - Path segments to extract parameters from
* @param paramsMap - Parameter mapping configuration
* @returns Object containing extracted parameters
*/
export function getMatchParams(
segments: string[],
paramsMap: RouterParamsMap
): Record<string, string> {
const params = new nullProtoObj() as Record<string, string>
for (const [index, name] of paramsMap) {
const segment = index < 0 ? segments.slice(-index).join('/') : segments[index] ?? ''
if (typeof name === 'string') {
params[name] = segment
} else {
const match = segment.match(name)
if (match?.groups) {
Object.assign(params, match.groups)
}
}
}
return params
}
/**
* Null prototype object constructor for property lookups.
* @returns Object with null prototype
*/
export const nullProtoObj = (() => {
function func(): void {
// Empty constructor
}
func.prototype = Object.create(null)
Object.freeze(func.prototype)
return func
})() as unknown as { new (): Record<string, unknown> }
/**
* Recursively search router tree for matching route.
* @param node - Current router node to search from
* @param method - HTTP method to match
* @param segments - Path segments to match
* @param index - Current segment index
* @returns Array of matching route data or undefined if no match
*/
export function searchTree<T>(
node: RouterNode<T>,
method: string,
segments: string[],
index: number
): Array<RouterMethodData<T>> | undefined {
if (index === segments.length) {
const match = node.methods?.[method] || node.methods?.['']
if (match) {
return match
}
const paramMatch = node.param?.methods?.[method] || node.param?.methods?.['']
if (paramMatch?.[0]?.paramsMap?.[paramMatch[0].paramsMap.length - 1]?.[2]) {
return paramMatch
}
const wildcardMatch = node.wildcard?.methods?.[method] || node.wildcard?.methods?.['']
return wildcardMatch?.[0]?.paramsMap?.[wildcardMatch[0].paramsMap.length - 1]?.[2]
? wildcardMatch
: undefined
}
const segment = segments[index]
if (!segment) {
return undefined
}
const staticNode = node.static?.[segment]
if (staticNode) {
const match = searchTree(staticNode, method, segments, index + 1)
if (match) {
return match
}
}
if (!node.param) {
return node.wildcard?.methods?.[method] || node.wildcard?.methods?.['']
}
const paramMatch = searchTree(node.param, method, segments, index + 1)
if (!paramMatch) {
return node.wildcard?.methods?.[method] || node.wildcard?.methods?.['']
}
if (node.param.hasRegexParam) {
const exactMatch = paramMatch.find((m) => m.paramsRegexp[index]?.test(segment))
return exactMatch ? [exactMatch] : undefined
}
return paramMatch
}
/**
* Split path into segments, removing empty segments.
* @param path - Path string to split
* @returns Array of non-empty path segments
*/
export function splitPath(path: string): string[] {
const [_, ...s] = path.split('/')
return s[s.length - 1] === '' ? s.slice(0, -1) : s
}