MCPcopy
hub / github.com/vercel/next.js / replaceDestination

Function replaceDestination

packages/next-routing/src/destination.ts:5–38  ·  view source on GitHub ↗
(
  destination: string,
  regexMatches: RegExpMatchArray | null,
  hasCaptures: Record<string, string>
)

Source from the content-addressed store, hash-verified

3 * with matches from the regex and has conditions
4 */
5export 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)

Callers 3

matchRouteFunction · 0.90
checkDynamicRoutesFunction · 0.90
resolveRoutesFunction · 0.90

Calls 2

replaceMethod · 0.65
entriesMethod · 0.45

Tested by

no test coverage detected