0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 11:51:01 +01:00
hono/deno_dist/router/trie-router/router.ts

29 lines
667 B
TypeScript
Raw Normal View History

2022-07-02 08:09:45 +02:00
import type { Result, Router } from '../../router.ts'
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> {
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) {
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)
}
match(method: string, path: string): Result<T> {
2022-07-02 08:09:45 +02:00
return this.node.search(method, path)
}
}