MCPcopy
hub / github.com/TanStack/router / interpolatePath

Function interpolatePath

packages/router-core/src/path.ts:270–428  ·  view source on GitHub ↗
({
  path,
  params,
  decoder,
  // `server` is marked @internal and stripped from .d.ts by `stripInternal`.
  // We avoid destructuring it in the function signature so the emitted
  // declaration doesn't reference a property that no longer exists.
  ...rest
}: InterpolatePathOptions)

Source from the content-addressed store, hash-verified

268 * - Supports `{-$optional}` segments, `{prefix{$id}suffix}` and `{$}` wildcards
269 */
270export function interpolatePath({
271 path,
272 params,
273 decoder,
274 // `server` is marked @internal and stripped from .d.ts by `stripInternal`.
275 // We avoid destructuring it in the function signature so the emitted
276 // declaration doesn't reference a property that no longer exists.
277 ...rest
278}: InterpolatePathOptions): InterPolatePathResult {
279 // Tracking if any params are missing in the `params` object
280 // when interpolating the path
281 let isMissingParams = false
282 const usedParams: Record<string, unknown> = Object.create(null)
283
284 if (!path || path === '/')
285 return { interpolatedPath: '/', usedParams, isMissingParams }
286 if (!path.includes('$'))
287 return { interpolatedPath: path, usedParams, isMissingParams }
288
289 if (isServer ?? rest.server) {
290 // Fast path for common templates like `/posts/$id` or `/files/$`.
291 // Braced segments (`{...}`) are more complex (prefix/suffix/optional) and are
292 // handled by the general parser below.
293 if (path.indexOf('{') === -1) {
294 const length = path.length
295 let cursor = 0
296 let joined = ''
297
298 while (cursor < length) {
299 // Skip slashes between segments. '/' code is 47
300 while (cursor < length && path.charCodeAt(cursor) === 47) cursor++
301 if (cursor >= length) break
302
303 const start = cursor
304 let end = path.indexOf('/', cursor)
305 if (end === -1) end = length
306 cursor = end
307
308 const part = path.substring(start, end)
309 if (!part) continue
310
311 // `$id` or `$` (splat). '$' code is 36
312 if (part.charCodeAt(0) === 36) {
313 if (part.length === 1) {
314 const splat = params._splat
315 usedParams._splat = splat
316 // TODO: Deprecate *
317 usedParams['*'] = splat
318
319 if (!splat) {
320 isMissingParams = true
321 continue
322 }
323
324 const value = encodeParam('_splat', params, decoder)
325 joined += '/' + value
326 } else {
327 const key = part.substring(1)

Callers 9

matchRoutesInternalMethod · 0.90
buildMethod · 0.90
path.test.tsFile · 0.90
setupFunction · 0.90
InterpolatePathLinkFunction · 0.90
RouteCompFunction · 0.90
InterpolatePathLinkFunction · 0.90

Calls 5

parseSegmentFunction · 0.90
encodeParamFunction · 0.85
includesMethod · 0.80
endsWithMethod · 0.80
createMethod · 0.65

Tested by

no test coverage detected