Files
openpanel/packages/remix-router/src/params-from-current-path/index.ts
Stefan Pejcic 09f9f9502d packages
2024-11-07 19:03:37 +01:00

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;
};