20 lines
561 B
TypeScript
20 lines
561 B
TypeScript
export function replaceUrlPart(currentPath: string, newPart: string): string {
|
|
const pathSegments = currentPath.split("/").filter((segment) => segment)
|
|
|
|
const newPathSegments = newPart
|
|
.replace(/\/$/, "")
|
|
.split("/")
|
|
.filter((segment) => segment)
|
|
|
|
const isFullPathReplacement = newPathSegments.length > 1
|
|
|
|
if (isFullPathReplacement) {
|
|
return `/${newPathSegments.join("/")}`
|
|
}
|
|
|
|
const updatedPathSegments = pathSegments.slice(1)
|
|
const updatedPath = `/${newPathSegments.concat(updatedPathSegments).join("/")}`
|
|
|
|
return updatedPath
|
|
}
|