From 5c49d24504c0e4d6367dd2398de93e087adb0e6f Mon Sep 17 00:00:00 2001 From: Soybean Date: Tue, 16 Jan 2024 02:07:31 +0800 Subject: [PATCH] perf(projects): perf code --- src/store/modules/route/shared.ts | 34 ++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/store/modules/route/shared.ts b/src/store/modules/route/shared.ts index 5cf7ff1b..22b4945e 100644 --- a/src/store/modules/route/shared.ts +++ b/src/store/modules/route/shared.ts @@ -48,20 +48,30 @@ function filterAuthRouteByRoles(route: ElegantConstRoute, roles: string[]) { } /** - * Sort routes by order + * sort route by order * - * @param routes An array of routes - * @returns A new array of routes sorted by order - * - * This function sorts the routes by their order property, which is a number. If the order property is missing or - * invalid, it is treated as 0. The routes with lower order values are placed before the routes with higher order - * values. The function also sorts the children routes recursively, if any. + * @param route route */ -export const sortRoutesByOrder = (routes: ElegantConstRoute[]): ElegantConstRoute[] => { - return routes - .sort((next, prev) => (Number(next.meta?.order) || 0) - (Number(prev.meta?.order) || 0)) - .map(route => (route.children ? { ...route, children: sortRoutesByOrder(route.children) } : route)); -}; +function sortRouteByOrder(route: ElegantConstRoute) { + if (route.children?.length) { + route.children.sort((next, prev) => (Number(next.meta?.order) || 0) - (Number(prev.meta?.order) || 0)); + route.children.forEach(sortRouteByOrder); + } + + return route; +} + +/** + * sort routes by order + * + * @param routes routes + */ +export function sortRoutesByOrder(routes: ElegantConstRoute[]) { + routes.sort((next, prev) => (Number(next.meta?.order) || 0) - (Number(prev.meta?.order) || 0)); + routes.forEach(sortRouteByOrder); + + return routes; +} /** * Get global menus by auth routes