@dazejs/framework
Version:
Daze.js - A powerful web framework for Node.js
35 lines • 1.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Trie = void 0;
const helpers_1 = require("./helpers");
const node_1 = require("./node");
class Trie {
constructor() {
this.routes = new Map();
}
add(route) {
for (const method of route.methods) {
if (!this.routes.has(method)) {
this.routes.set(method, new node_1.Node());
}
this.register(this.routes.get(method), route);
}
}
register(rootNode, route) {
rootNode.insert(route, route.pieces, 0);
}
match(request) {
const _method = request.getMethod() || '';
const _path = request.getPath();
const rootNode = this.routes.get(_method);
if (!rootNode)
return null;
const keys = (0, helpers_1.parsePattern)(_path);
const result = rootNode.search(keys, 0);
if (result)
return result.route || null;
return null;
}
}
exports.Trie = Trie;
//# sourceMappingURL=trie.js.map