Skip to content

Commit

Permalink
Move parentRouteId logic to a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarkow committed Dec 7, 2022
1 parent 39c8268 commit b1e2032
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions packages/remix-dev/config/routesConvention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ export function defineConventionalRoutes(
});

let routeIds = Object.keys(files).sort(byLongestFirst);
let parentRouteIds: { [routeId: string]: string | undefined } = {};
for (let id of routeIds) {
parentRouteIds[id] = findParentRouteId(routeIds, id);
}
let parentRouteIds = getParentRouteIds(routeIds);

let uniqueRoutes = new Map<string, string>();

Expand Down Expand Up @@ -197,11 +194,16 @@ export function createRoutePath(partialRouteId: string): string | undefined {
return result || undefined;
}

function findParentRouteId(
routeIds: string[],
childRouteId: string
): string | undefined {
return routeIds.find((id) => childRouteId.startsWith(`${id}/`));
function getParentRouteIds(
routeIds: string[]
): Record<string, string | undefined> {
return routeIds.reduce<Record<string, string | undefined>>(
(parentRouteIds, childRouteId) => ({
...parentRouteIds,
[childRouteId]: routeIds.find((id) => childRouteId.startsWith(`${id}/`)),
}),
{}
);
}

function byLongestFirst(a: string, b: string): number {
Expand Down

0 comments on commit b1e2032

Please sign in to comment.