75 lines
2.0 KiB
Plaintext
75 lines
2.0 KiB
Plaintext
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
const require_router_utils = require("../utils.js");
|
|
const require_router_reg_exp_router_node = require("./node.js");
|
|
//#region src/router/reg-exp-router/trie.ts
|
|
var Trie = class {
|
|
#context = { varIndex: 0 };
|
|
#root = new require_router_reg_exp_router_node.Node();
|
|
#index = 0;
|
|
paths = require_router_utils.createNullObject();
|
|
insert(path, isStatic) {
|
|
if (isStatic) {
|
|
this.#root.insert(path.split(""), 0, [], this.#context, true);
|
|
return;
|
|
}
|
|
const paramAssoc = [];
|
|
const groups = [];
|
|
let markedPath = path;
|
|
for (let i = 0;;) {
|
|
let replaced = false;
|
|
markedPath = markedPath.replace(/\{[^}]+\}/g, (m) => {
|
|
const mark = `@\\${i}`;
|
|
groups[i] = [mark, m];
|
|
i++;
|
|
replaced = true;
|
|
return mark;
|
|
});
|
|
if (!replaced) break;
|
|
}
|
|
/**
|
|
* - pattern (:label, :label{0-9]+}, ...)
|
|
* - /* wildcard
|
|
* - character
|
|
*/
|
|
const tokens = markedPath.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, this.#index, paramAssoc, this.#context, false);
|
|
this.paths[path] = [this.#index++, 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 (handlerIndex !== void 0) {
|
|
indexReplacementMap[++captureIndex] = Number(handlerIndex);
|
|
return "$()";
|
|
}
|
|
if (paramIndex !== void 0) {
|
|
paramReplacementMap[Number(paramIndex)] = ++captureIndex;
|
|
return "";
|
|
}
|
|
return "";
|
|
});
|
|
return [
|
|
new RegExp(`^${regexp}`),
|
|
indexReplacementMap,
|
|
paramReplacementMap
|
|
];
|
|
}
|
|
};
|
|
//#endregion
|
|
exports.Trie = Trie;
|