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