Skip to content

Commit

Permalink
feat(projects): add getRouteName transform
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Nov 5, 2023
1 parent e36c1fe commit 930513c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 28 deletions.
57 changes: 36 additions & 21 deletions examples/template-vue/src/router/elegant/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,41 @@ function transformElegantRouteToVueRoute(
return vueRoutes;
}

export function getRoutePath(key: RouteKey) {
const routeMap: RouteMap = {
"root": "/",
"not-found": "/:pathMatch(.*)*",
"403": "/403",
"404": "/404",
"500": "/500",
"about": "/about",
"list": "/list",
"list_detail": "/list/detail",
"list_home": "/list/home",
"multi-menu": "/multi-menu",
"multi-menu_first": "/multi-menu/first",
"multi-menu_first_child": "/multi-menu/first/child",
"multi-menu_second": "/multi-menu/second",
"multi-menu_second_child": "/multi-menu/second/child",
"multi-menu_second_child_home": "/multi-menu/second/child/home",
"user": "/user/:id"
};

return routeMap[key];
const routeMap: RouteMap = {
"root": "/",
"not-found": "/:pathMatch(.*)*",
"403": "/403",
"404": "/404",
"500": "/500",
"about": "/about",
"list": "/list",
"list_detail": "/list/detail",
"list_home": "/list/home",
"multi-menu": "/multi-menu",
"multi-menu_first": "/multi-menu/first",
"multi-menu_first_child": "/multi-menu/first/child",
"multi-menu_second": "/multi-menu/second",
"multi-menu_second_child": "/multi-menu/second/child",
"multi-menu_second_child_home": "/multi-menu/second/child/home",
"user": "/user/:id"
};

/**
* get route path by route name
* @param name route name
*/
export function getRoutePath(name: RouteKey) {
return routeMap[name] || null;
}

/**
* get route name by route path
* @param path route path
*/
export function getRouteName(path: RouteMap[RouteKey]) {
const routeEntries = Object.entries(routeMap) as [RouteKey, RouteMap[RouteKey]][];

const routeName = routeEntries.find(([, routePath]) => routePath === path)?.[0];

return routeName || null;
}
26 changes: 19 additions & 7 deletions packages/vue/src/core/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,28 @@ function transformElegantRouteToVueRoute(
return vueRoutes;
}
const routeMap: RouteMap = {
${allEntries.map(([routeName, routePath]) => `"${routeName}": "${routePath}"`).join(',\n ')}
};
/**
* get route path by route name
* @param name route name
*/
export function getRoutePath(name: RouteKey) {
return routeMap[name] || null;
}
/**
* get route path by route key
* @param key route key
* get route name by route path
* @param path route path
*/
export function getRoutePath(key: RouteKey) {
const routeMap: RouteMap = {
${allEntries.map(([routeName, routePath]) => `"${routeName}": "${routePath}"`).join(',\n ')}
};
export function getRouteName(path: RouteMap[RouteKey]) {
const routeEntries = Object.entries(routeMap) as [RouteKey, RouteMap[RouteKey]][];
const routeName = routeEntries.find(([, routePath]) => routePath === path)?.[0];
return routeMap[key];
return routeName || null;
}
`;

Expand Down

0 comments on commit 930513c

Please sign in to comment.