Skip to content

Commit

Permalink
feat(projects): add tree route transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Oct 22, 2023
1 parent 6f8fb94 commit 617143b
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
50 changes: 50 additions & 0 deletions examples/template-vue/src/router/elegant/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,53 @@ function isView(component: string) {
function getViewName(component: string) {
return component.replace(VIEW_PREFIX, '');
}

/**
* transform elegant route to tree route
* @param routes elegant routes
*/
export function transformElegantRouteToTreeRoute(routes: AutoRoute[]) {
const treeRoutes = routes.map(route => {
const { children = [], ...rest } = route;

const treeRoute: AutoRoute = { ...rest, children: [] };

const treeMap = new Map<string, AutoRoute>();

children.forEach(child => {
treeMap.set(child.name as string, { ...child });
});

const treeChildren: AutoRoute[] = [];

children.forEach(child => {
// current route level is 2, if has parent, then level is 3 or more
const hasParent = (child.name as string).split('_').length > 2;

const current = treeMap.get(child.name as string)!;

if (hasParent) {
const parentName = (child.name as string).split('_').slice(0, -1).join('_');

const parent = treeMap.get(parentName);

if (parent) {
if (!parent.children) {
parent.children = [];
}
parent.children.push(current);
}
} else {
treeChildren.push(current);
}
});

if (treeChildren.length) {
treeRoute.children = treeChildren;
}

return treeRoute;
});

return treeRoutes;
}
50 changes: 50 additions & 0 deletions packages/vue/src/context/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,56 @@ function isView(component: string) {
function getViewName(component: string) {
return component.replace(VIEW_PREFIX, '');
}
/**
* transform elegant route to tree route
* @param routes elegant routes
*/
export function transformElegantRouteToTreeRoute(routes: AutoRoute[]) {
const treeRoutes = routes.map(route => {
const { children = [], ...rest } = route;
const treeRoute: AutoRoute = { ...rest, children: [] };
const treeMap = new Map<string, AutoRoute>();
children.forEach(child => {
treeMap.set(child.name as string, { ...child });
});
const treeChildren: AutoRoute[] = [];
children.forEach(child => {
// current route level is 2, if has parent, then level is 3 or more
const hasParent = (child.name as string).split('_').length > 2;
const current = treeMap.get(child.name as string)!;
if (hasParent) {
const parentName = (child.name as string).split('_').slice(0, -1).join('_');
const parent = treeMap.get(parentName);
if (parent) {
if (!parent.children) {
parent.children = [];
}
parent.children.push(current);
}
} else {
treeChildren.push(current);
}
});
if (treeChildren.length) {
treeRoute.children = treeChildren;
}
return treeRoute;
});
return treeRoutes;
}
`;

return code;
Expand Down

0 comments on commit 617143b

Please sign in to comment.