mirror of
https://github.com/stefanpejcic/openpanel
synced 2025-06-26 18:28:26 +00:00
24 lines
661 B
TypeScript
24 lines
661 B
TypeScript
export const paramsFromCurrentPath = (
|
|
pathname: string,
|
|
matchingRoute: string,
|
|
) => {
|
|
const params: Record<string, string> = {};
|
|
|
|
// remove leading and trailing slashes
|
|
const sanitizedMatchingRoute = matchingRoute.replace(/^\/|\/$/g, "");
|
|
const sanitizedPathname = pathname.replace(/^\/|\/$/g, "");
|
|
|
|
const matchingRouteParts = sanitizedMatchingRoute.split("/");
|
|
const pathnameParts = sanitizedPathname.split("/");
|
|
|
|
matchingRouteParts.forEach((part, index) => {
|
|
if (part.startsWith(":")) {
|
|
if (pathnameParts[index]?.length > 0) {
|
|
params[part.replace(":", "")] = pathnameParts[index];
|
|
}
|
|
}
|
|
});
|
|
|
|
return params;
|
|
};
|