exstack
Version:
A utility library designed to simplify and enhance Express.js applications.
81 lines (79 loc) • 2.33 kB
JavaScript
//#region src/router/trie-tree/utils.ts
const patternCache = {};
const getPattern = (label, next) => {
if (label === "*") return "*";
const match = label.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
if (match) {
const cacheKey = `${label}#${next}`;
if (!patternCache[cacheKey]) if (match[2]) patternCache[cacheKey] = next && next[0] !== ":" && next[0] !== "*" ? [
cacheKey,
match[1],
/* @__PURE__ */ new RegExp(`^${match[2]}(?=/${next})`)
] : [
label,
match[1],
/* @__PURE__ */ new RegExp(`^${match[2]}$`)
];
else patternCache[cacheKey] = [
label,
match[1],
true
];
return patternCache[cacheKey];
}
return null;
};
const extractGroupsFromPath = (path) => {
const groups = [];
path = path.replace(/\{[^}]+\}/g, (match, index) => {
const mark = `@${index}`;
groups.push([mark, match]);
return mark;
});
return {
groups,
path
};
};
const splitRoutingPath = (routePath) => {
const { groups, path } = extractGroupsFromPath(routePath);
const paths = splitPath(path);
return replaceGroupMarks(paths, groups);
};
const replaceGroupMarks = (paths, groups) => {
for (let i = groups.length - 1; i >= 0; i--) {
const [mark] = groups[i];
for (let j = paths.length - 1; j >= 0; j--) if (paths[j].includes(mark)) {
paths[j] = paths[j].replace(mark, groups[i][1]);
break;
}
}
return paths;
};
const splitPath = (path) => {
const paths = path.split("/");
if (paths[0] === "") paths.shift();
return paths;
};
const checkOptionalParameter = (path) => {
if (path.charCodeAt(path.length - 1) !== 63 || !path.includes(":")) return null;
const segments = path.split("/");
const results = [];
let basePath = "";
segments.forEach((segment) => {
if (segment !== "" && !/\:/.test(segment)) basePath += "/" + segment;
else if (/\:/.test(segment)) if (/\?/.test(segment)) {
if (results.length === 0 && basePath === "") results.push("/");
else results.push(basePath);
const optionalSegment = segment.replace("?", "");
basePath += "/" + optionalSegment;
results.push(basePath);
} else basePath += "/" + segment;
});
return results.filter((v, i, a) => a.indexOf(v) === i);
};
//#endregion
exports.checkOptionalParameter = checkOptionalParameter;
exports.getPattern = getPattern;
exports.splitPath = splitPath;
exports.splitRoutingPath = splitRoutingPath;