|
1 | | -/** |
2 | | - * Utility functions for traversing router trees. |
3 | | - * Consolidates common recursive tree operations used across the codebase. |
4 | | - */ |
5 | | - |
6 | 1 | import type { AppDefinition, RouteDefinition, RouterDefinition } from "./types" |
7 | 2 |
|
8 | | -/** |
9 | | - * Traverses all routers in a tree, calling the visitor function for each. |
10 | | - * Returns early if visitor returns a non-undefined value. |
11 | | - */ |
12 | | -function traverseRouters<T>( |
13 | | - routers: RouterDefinition[], |
14 | | - visitor: (router: RouterDefinition) => T | undefined, |
15 | | -): T | undefined { |
16 | | - for (const router of routers) { |
17 | | - const result = visitor(router) |
18 | | - if (result !== undefined) { |
19 | | - return result |
20 | | - } |
21 | | - const childResult = traverseRouters(router.children, visitor) |
22 | | - if (childResult !== undefined) { |
23 | | - return childResult |
24 | | - } |
25 | | - } |
26 | | - return undefined |
27 | | -} |
28 | | - |
29 | | -/** |
30 | | - * Finds a router matching the predicate across all apps. |
31 | | - */ |
| 3 | +/** Finds the first router matching a condition across all apps. */ |
32 | 4 | export function findRouter( |
33 | 5 | apps: AppDefinition[], |
34 | 6 | predicate: (router: RouterDefinition) => boolean, |
35 | 7 | ): RouterDefinition | undefined { |
| 8 | + function findIn(routers: RouterDefinition[]): RouterDefinition | undefined { |
| 9 | + for (const r of routers) { |
| 10 | + if (predicate(r)) return r |
| 11 | + const found = findIn(r.children) |
| 12 | + if (found) return found |
| 13 | + } |
| 14 | + return undefined |
| 15 | + } |
36 | 16 | for (const app of apps) { |
37 | | - const found = traverseRouters(app.routers, (router) => |
38 | | - predicate(router) ? router : undefined, |
39 | | - ) |
| 17 | + const found = findIn(app.routers) |
40 | 18 | if (found) return found |
41 | 19 | } |
42 | 20 | return undefined |
43 | 21 | } |
44 | 22 |
|
45 | | -/** |
46 | | - * Collects all routes from a list of routers, including nested children. |
47 | | - */ |
48 | | -function collectRoutesFromRouters( |
49 | | - routers: RouterDefinition[], |
50 | | -): RouteDefinition[] { |
51 | | - return routers.flatMap((router) => [ |
52 | | - ...router.routes, |
53 | | - ...collectRoutesFromRouters(router.children), |
54 | | - ]) |
| 23 | +/** Collects all routes from all apps, including nested router routes. */ |
| 24 | +export function collectRoutes(apps: AppDefinition[]): RouteDefinition[] { |
| 25 | + function fromRouters(routers: RouterDefinition[]): RouteDefinition[] { |
| 26 | + return routers.flatMap((r) => [...r.routes, ...fromRouters(r.children)]) |
| 27 | + } |
| 28 | + return apps.flatMap((app) => [...app.routes, ...fromRouters(app.routers)]) |
55 | 29 | } |
56 | 30 |
|
57 | | -/** |
58 | | - * Collects all routes from all apps, including nested router routes. |
59 | | - */ |
60 | | -export function collectRoutes(apps: AppDefinition[]): RouteDefinition[] { |
61 | | - return apps.flatMap((app) => [ |
62 | | - ...app.routes, |
63 | | - ...collectRoutesFromRouters(app.routers), |
64 | | - ]) |
| 31 | +/** Counts all routes in a router, including nested routers. */ |
| 32 | +export function countRoutesInRouter(router: RouterDefinition): number { |
| 33 | + return ( |
| 34 | + router.routes.length + |
| 35 | + router.children.reduce((sum, child) => sum + countRoutesInRouter(child), 0) |
| 36 | + ) |
65 | 37 | } |
66 | 38 |
|
67 | | -/** |
68 | | - * Counts total routers across all apps. |
69 | | - */ |
| 39 | +/** Counts total routers across all apps. */ |
70 | 40 | export function countRouters(apps: AppDefinition[]): number { |
71 | | - let count = 0 |
72 | | - for (const app of apps) { |
73 | | - traverseRouters(app.routers, () => { |
74 | | - count++ |
75 | | - return undefined |
76 | | - }) |
| 41 | + function count(routers: RouterDefinition[]): number { |
| 42 | + return routers.reduce((sum, r) => sum + 1 + count(r.children), 0) |
77 | 43 | } |
78 | | - return count |
| 44 | + return apps.reduce((sum, app) => sum + count(app.routers), 0) |
79 | 45 | } |
0 commit comments