( destination: string, regexMatches: RegExpMatchArray | null, hasCaptures: Record<string, string> )
| 3 | * with matches from the regex and has conditions |
| 4 | */ |
| 5 | export function replaceDestination( |
| 6 | destination: string, |
| 7 | regexMatches: RegExpMatchArray | null, |
| 8 | hasCaptures: Record<string, string> |
| 9 | ): string { |
| 10 | let result = destination |
| 11 | |
| 12 | // Replace numbered captures from regex ($1, $2, etc.) |
| 13 | if (regexMatches) { |
| 14 | // Replace numbered groups (skip index 0 which is the full match) |
| 15 | for (let i = 1; i < regexMatches.length; i++) { |
| 16 | const value = regexMatches[i] |
| 17 | if (value !== undefined) { |
| 18 | result = result.replace(new RegExp(`\\$${i}`, 'g'), value) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // Replace named groups ($name) |
| 23 | if (regexMatches.groups) { |
| 24 | for (const [name, value] of Object.entries(regexMatches.groups)) { |
| 25 | if (value !== undefined) { |
| 26 | result = result.replace(new RegExp(`\\$${name}`, 'g'), value) |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // Replace named captures from has conditions |
| 33 | for (const [name, value] of Object.entries(hasCaptures)) { |
| 34 | result = result.replace(new RegExp(`\\$${name}`, 'g'), value) |
| 35 | } |
| 36 | |
| 37 | return result |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Checks if a destination is an external rewrite (starts with http/https) |
no test coverage detected