* Matches dynamic routes and extracts route parameters
( pathname: string, route: Route )
| 229 | * Matches dynamic routes and extracts route parameters |
| 230 | */ |
| 231 | function matchDynamicRoute( |
| 232 | pathname: string, |
| 233 | route: Route |
| 234 | ): { |
| 235 | matched: boolean |
| 236 | params?: Record<string, string> |
| 237 | regexMatches?: RegExpMatchArray |
| 238 | } { |
| 239 | const regex = new RegExp(route.sourceRegex) |
| 240 | const match = pathname.match(regex) |
| 241 | |
| 242 | if (!match) { |
| 243 | return { matched: false } |
| 244 | } |
| 245 | |
| 246 | const params: Record<string, string> = {} |
| 247 | |
| 248 | // Add numbered matches |
| 249 | for (let i = 1; i < match.length; i++) { |
| 250 | if (match[i] !== undefined) { |
| 251 | params[String(i)] = match[i] |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // Add named matches |
| 256 | if (match.groups) { |
| 257 | Object.assign(params, match.groups) |
| 258 | } |
| 259 | |
| 260 | return { matched: true, params, regexMatches: match } |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Applies headers from onMatch routes |
no test coverage detected