just testing the environment
Some checks failed
Tests / test (push) Has been cancelled

This commit is contained in:
tom.hempel
2026-05-20 07:10:08 +02:00
parent 2a11dfd0d1
commit 06fc134299
56 changed files with 1890 additions and 361 deletions

View File

@ -1,17 +1,37 @@
const routes = [];
let currentCleanup = null;
/** @returns {{ regex: RegExp, paramNames: string[] }} */
export function compileRoute(pattern) {
const paramNames = [];
const parts = pattern.replace(/\/:([^/]+)/g, (_, name) => {
paramNames.push(name);
return '/([^/]+)';
});
return { regex: new RegExp('^' + parts + '$'), paramNames };
}
/** Match a path against a string route pattern; returns params or null. */
export function matchRoute(pattern, path) {
const { regex, paramNames } = compileRoute(pattern);
const match = path.match(regex);
if (!match) {
return null;
}
const params = {};
paramNames.forEach((name, i) => {
params[name] = decodeURIComponent(match[i + 1]);
});
return params;
}
export function addRoute(pattern, handler) {
let regex;
const paramNames = [];
let paramNames = [];
if (pattern instanceof RegExp) {
regex = pattern;
} else {
const parts = pattern.replace(/\/:([^/]+)/g, (_, name) => {
paramNames.push(name);
return '/([^/]+)';
});
regex = new RegExp('^' + parts + '$');
({ regex, paramNames } = compileRoute(pattern));
}
routes.push({ regex, paramNames, handler });
}