(url: string, placement: VersionPlacement, newVersion: string)
| 58 | } |
| 59 | |
| 60 | export function replaceVersion(url: string, placement: VersionPlacement, newVersion: string) { |
| 61 | if (!placement) return url; |
| 62 | |
| 63 | const parsed = parseUrl(url); |
| 64 | if (!parsed) return url; |
| 65 | |
| 66 | if (placement.type === 'query') { |
| 67 | parsed.searchParams.set(placement.paramName, newVersion); |
| 68 | return parsed.href; |
| 69 | } |
| 70 | |
| 71 | if (placement.type === 'path') { |
| 72 | const segments = parsed.pathname.split('/'); |
| 73 | segments[placement.segmentIndex] = newVersion; |
| 74 | parsed.pathname = segments.join('/'); |
| 75 | return parsed.href; |
| 76 | } |
| 77 | |
| 78 | if (placement.type === 'subdomain') { |
| 79 | const labels = parsed.hostname.split('.'); |
| 80 | labels[placement.labelIndex] = newVersion; |
| 81 | parsed.hostname = labels.join('.'); |
| 82 | return parsed.href; |
| 83 | } |
| 84 | |
| 85 | if (placement.type === 'hash') { |
| 86 | const prefix = parsed.hash.startsWith('#/') ? '#/' : '#'; |
| 87 | const hashContent = parsed.hash.replace(/^#\/?/, ''); |
| 88 | const segments = hashContent.split('/'); |
| 89 | segments[placement.segmentIndex] = newVersion; |
| 90 | parsed.hash = prefix + segments.join('/'); |
| 91 | return parsed.href; |
| 92 | } |
| 93 | |
| 94 | return url; |
| 95 | } |
| 96 | |
| 97 | export function matchesTemplate(templateUrl: string, concreteUrl: string, placement: VersionPlacement) { |
| 98 | if (!placement) return sameOrigin(templateUrl, concreteUrl); |
no test coverage detected