UNPKG

hono

Version:

Web framework built on Web Standards

60 lines (59 loc) 1.71 kB
// src/router/reg-exp-router/trie.ts import { Node } from "./node.js"; var Trie = class { context = { varIndex: 0 }; root = new Node(); insert(path, index, pathErrorCheckOnly) { const paramAssoc = []; const groups = []; for (let i = 0; ; ) { let replaced = false; path = path.replace(/\{[^}]+\}/g, (m) => { const mark = `@\\${i}`; groups[i] = [mark, m]; i++; replaced = true; return mark; }); if (!replaced) { break; } } const tokens = path.match(/(?::[^\/]+)|(?:\/\*$)|./g) || []; for (let i = groups.length - 1; i >= 0; i--) { const [mark] = groups[i]; for (let j = tokens.length - 1; j >= 0; j--) { if (tokens[j].indexOf(mark) !== -1) { tokens[j] = tokens[j].replace(mark, groups[i][1]); break; } } } this.root.insert(tokens, index, paramAssoc, this.context, pathErrorCheckOnly); return paramAssoc; } buildRegExp() { let regexp = this.root.buildRegExpStr(); if (regexp === "") { return [/^$/, [], []]; } let captureIndex = 0; const indexReplacementMap = []; const paramReplacementMap = []; regexp = regexp.replace(/#(\d+)|@(\d+)|\.\*\$/g, (_, handlerIndex, paramIndex) => { if (typeof handlerIndex !== "undefined") { indexReplacementMap[++captureIndex] = Number(handlerIndex); return "$()"; } if (typeof paramIndex !== "undefined") { paramReplacementMap[Number(paramIndex)] = ++captureIndex; return ""; } return ""; }); return [new RegExp(`^${regexp}`), indexReplacementMap, paramReplacementMap]; } }; export { Trie };