* Inserts a new route into the route tree. * The route is broken down into segments, and each segment is added to the tree. * Parameterized segments (e.g., :id) are normalized to wildcards (*) for matching purposes. * * @param route - The route path to insert into the tree. * @param m
(route: string, metadata: RouteTreeNodeMetadataWithoutRoute & AdditionalMetadata)
| 112 | * @param metadata - Metadata associated with the route, excluding the route path itself. |
| 113 | */ |
| 114 | insert(route: string, metadata: RouteTreeNodeMetadataWithoutRoute & AdditionalMetadata): void { |
| 115 | let node = this.root; |
| 116 | const segments = this.getPathSegments(route); |
| 117 | const normalizedSegments: string[] = []; |
| 118 | |
| 119 | for (const segment of segments) { |
| 120 | // Replace parameterized segments (e.g., :id) with a wildcard (*) for matching |
| 121 | const normalizedSegment = segment[0] === ':' ? '*' : segment; |
| 122 | let childNode = node.children.get(normalizedSegment); |
| 123 | if (!childNode) { |
| 124 | childNode = this.createEmptyRouteTreeNode(); |
| 125 | node.children.set(normalizedSegment, childNode); |
| 126 | } |
| 127 | |
| 128 | node = childNode; |
| 129 | normalizedSegments.push(normalizedSegment); |
| 130 | } |
| 131 | |
| 132 | // At the leaf node, store the full route and its associated metadata |
| 133 | node.metadata = { |
| 134 | ...metadata, |
| 135 | route: addLeadingSlash(normalizedSegments.join('/')), |
| 136 | }; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Matches a given route against the route tree and returns the best matching route's metadata. |
no test coverage detected