Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support static prefixes & suffixes in dynamic path segments #709

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/react-router/src/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ export type Split<S, TIncludeTrailingSlash = true> = S extends unknown
: never

export type ParsePathParams<T extends string> = keyof {
[K in Trim<Split<T>[number], '_'> as K extends `$${infer L}` ? L : never]: K
[K in Trim<
Split<T>[number],
'_'
> as K extends `${string}$${infer L}$${string}`
? L
: K extends `${string}$${infer L}`
? L
: never]: K
}

export type Join<T, Delimiter extends string = '/'> = T extends []
Expand Down
21 changes: 17 additions & 4 deletions packages/react-router/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function parsePathname(pathname?: string): Segment[] {
}
}

if (part.charAt(0) === '$') {
if (part.includes('$')) {
return {
type: 'param',
value: part,
Expand Down Expand Up @@ -138,7 +138,8 @@ export function interpolatePath(
}

if (segment.type === 'param') {
return params![segment.value.substring(1)] ?? 'undefined'
const [p, v, ...s] = segment.value.split('$')
return `${p!}${params![v!] ?? ''}${s.join('$')}`
}

return segment.value
Expand Down Expand Up @@ -242,8 +243,20 @@ export function matchByPath(
if (baseSegment?.value === '/') {
return false
}
if (baseSegment.value.charAt(0) !== '$') {
params[routeSegment.value.substring(1)] = baseSegment.value

const [p, v, ...suf] = routeSegment.value.split('$')
const s = suf.join('$')
const staticMatch = matchLocation.caseSensitive
? baseSegment.value.startsWith(p!) && baseSegment.value.endsWith(s)
: baseSegment.value.toLowerCase().startsWith(p!.toLowerCase()) &&
baseSegment.value.toLowerCase().endsWith(s.toLowerCase())
if (!staticMatch) {
return false
}

const paramValue = baseSegment.value.slice(p!.length, -s.length)
if (!paramValue.startsWith('$')) {
params[v!] = paramValue
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions packages/react-router/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,17 @@ export type DeepAwaited<T> = T extends Promise<infer A>
? { [K in A]: DeepAwaited<B> }
: T

type PathParamMaskSegment<S extends string> =
S extends `${infer P}$${string}$${infer S}`
? `${P}${string}${S}`
: S extends `${infer P}$${string}`
? `${P}${string}`
: S

export type PathParamMask<TRoutePath extends string> =
TRoutePath extends `${infer L}/$${infer C}/${infer R}`
? PathParamMask<`${L}/${string}/${R}`>
: TRoutePath extends `${infer L}/$${infer C}`
? PathParamMask<`${L}/${string}`>
: TRoutePath
TRoutePath extends `${infer T}/${infer U}`
? `${PathParamMaskSegment<T>}/${PathParamMask<U>}`
: PathParamMaskSegment<TRoutePath>

export type Timeout = ReturnType<typeof setTimeout>

Expand Down
Loading