-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathcomputeViewNameFromParams.ts
More file actions
35 lines (30 loc) · 1.07 KB
/
computeViewNameFromParams.ts
File metadata and controls
35 lines (30 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
export function computeViewNameFromParams(
pathname: string,
params: Record<string, string | string[] | undefined>
): string {
if (!params || Object.keys(params).length === 0) {
return pathname
}
let viewName = pathname
// Sort params by value length descending to replace longer values first.
// Prevents partial replacements (e.g., replacing '1' before '123').
const sortedParams = Object.entries(params).sort((a, b) => {
const aLen = Array.isArray(a[1]) ? a[1].join('/').length : (a[1]?.length ?? 0)
const bLen = Array.isArray(b[1]) ? b[1].join('/').length : (b[1]?.length ?? 0)
return bLen - aLen
})
for (const [paramName, paramValue] of sortedParams) {
if (paramValue === undefined) {
continue
}
if (Array.isArray(paramValue)) {
const joinedValue = paramValue.join('/')
if (joinedValue && viewName.includes(joinedValue)) {
viewName = viewName.replace(joinedValue, `[...${paramName}]`)
}
} else if (paramValue) {
viewName = viewName.replace(paramValue, `[${paramName}]`)
}
}
return viewName
}