UNPKG

@dazejs/framework

Version:

Daze.js - A powerful web framework for Node.js

69 lines 2.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Node = void 0; const helpers_1 = require("./helpers"); class Node { constructor(key, type) { this.children = []; this.priority = 0; this.key = key; this.type = type; } matchChild(key) { for (const child of this.children) { if ((0, helpers_1.isMatchNodeWithType)(child, key)) { return child; } } return null; } matchChildren(key) { const nodes = []; for (const child of this.children) { if ((0, helpers_1.isMatchNodeWithType)(child, key)) { nodes.push(child); } } return nodes; } insert(route, pieces = [], height = 0) { if (pieces.length === height) { this.route = route; return; } const keyObj = pieces[height]; let child = this.matchChild(keyObj.key); if (!child || child.type === 'all') { child = new Node(keyObj.key, keyObj.type); this.children.push(child); } child.priority++; child.insert(route, pieces, height + 1); this.reorderChildren(); } reorderChildren() { this.children.sort((a, b) => b.priority - a.priority); this.children.sort((_a, b) => b.type === 'all' ? -1 : 1); } search(keys = [], height = 0) { if (keys.length === height) { if (!this.route) return null; return this; } const key = keys[height]; const children = this.matchChildren(key); for (const child of children) { if (child.key && child.type === 'all') { if (child.route) return child; } const res = child.search(keys, height + 1); if (res) return res; } return null; } } exports.Node = Node; //# sourceMappingURL=node.js.map