-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.ts
85 lines (76 loc) · 2.33 KB
/
router.ts
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
import { Route } from "./types.ts";
/**
* Core Router for adding and finding routes
* @Author metadream
* @Since 2022-11-09
*
* @example /literal
* @example /:user
* @example /:users?
* @example /:user(\\d+)
* @example /*
*/
export class Router {
private SORT_RULE = ["", "*", ":"];
private routes: Route[] = [];
private sorted = false;
/**
* Add a route
* @param {string} method
* @param {string|RegExp} path
* @param {Function} handler
*/
add(route: Route): void {
route.pattern = this.parse(route.path);
this.routes.push(route);
}
/**
* Find a route
* @param {string} method
* @param {string} url
* @returns
*/
find(method: string, url: string): Route | undefined {
if (!this.sorted) {
this.sorted = true;
this.sortRoutes();
}
for (const route of this.routes) {
if (route.method && route.method !== method) continue;
if (!route.pattern) continue;
const matches = route.pattern.exec(url);
if (matches) {
const g = matches.groups;
route.params = {};
if (g) for (const k in g) route.params[k] = g[k];
return route;
}
}
}
// Sort routes by specific rule
private sortRoutes(): void {
this.routes.sort((a: Route, b: Route) => {
const aLen = a.path.length;
const bLen = b.path.length;
const maxLen = Math.max(aLen, bLen);
for (let i = 0; i < maxLen; i++) {
const aChar = a.path.charAt(i);
const bChar = b.path.charAt(i);
if (aChar == bChar) continue;
const aIndex = this.SORT_RULE.findIndex((v) => v == aChar);
const bIndex = this.SORT_RULE.findIndex((v) => v == bChar);
return aIndex - bIndex;
}
return 0;
});
}
// Parse route path to regex pattern
private parse(pattern: string | RegExp): RegExp {
return pattern instanceof RegExp ? pattern : new RegExp(
"^" + pattern
.replace(/\/\*($|\/)/g, "/(?<wildcard>.*)$1")
.replace(/:(\w+)(\(\S+\))?/g, (_, k, r) => `(?<${k}>${r ? r : "([^/]+?)"})`) +
"$",
);
}
}