2022-07-02 06:09:45 +00:00
|
|
|
import type { Result, Router } from '../../router.ts'
|
2022-09-20 10:05:13 +00:00
|
|
|
import { checkOptionalParameter } from '../../utils/url.ts'
|
2022-07-02 06:09:45 +00:00
|
|
|
import { Node } from './node.ts'
|
|
|
|
|
|
|
|
export class TrieRouter<T> implements Router<T> {
|
|
|
|
node: Node<T>
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.node = new Node()
|
|
|
|
}
|
|
|
|
|
|
|
|
add(method: string, path: string, handler: T) {
|
2022-09-20 10:05:13 +00:00
|
|
|
const results = checkOptionalParameter(path)
|
|
|
|
if (results) {
|
|
|
|
for (const p of results) {
|
|
|
|
this.node.insert(method, p, handler)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-02 06:09:45 +00:00
|
|
|
this.node.insert(method, path, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
match(method: string, path: string): Result<T> | null {
|
|
|
|
return this.node.search(method, path)
|
|
|
|
}
|
|
|
|
}
|