[WEB-4429] fix: url path generation #7317

This commit is contained in:
Prateek Shourya 2025-07-02 19:57:10 +05:30 committed by GitHub
parent e624a17868
commit 6ea24bfdcd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 50 additions and 9 deletions

View file

@ -318,3 +318,39 @@ export const copyTextToClipboard = async (text: string): Promise<void> => {
}
await navigator.clipboard.writeText(text);
};
/**
* @description Joins URL path segments properly, removing duplicate slashes
* @param {...string} segments - URL path segments to join
* @returns {string} Properly joined URL path
* @example
* joinUrlPath("/workspace", "/projects") => "/workspace/projects"
* joinUrlPath("/workspace", "projects") => "/workspace/projects"
* joinUrlPath("workspace", "projects") => "workspace/projects"
* joinUrlPath("/workspace/", "/projects/") => "/workspace/projects/"
*/
export const joinUrlPath = (...segments: string[]): string => {
if (segments.length === 0) return "";
// Filter out empty segments
const validSegments = segments.filter((segment) => segment !== "");
if (validSegments.length === 0) return "";
// Join segments and normalize slashes
const joined = validSegments
.map((segment, index) => {
// Remove leading slashes from all segments except the first
if (index > 0) {
segment = segment.replace(/^\/+/, "");
}
// Remove trailing slashes from all segments except the last
if (index < validSegments.length - 1) {
segment = segment.replace(/\/+$/, "");
}
return segment;
})
.join("/");
// Clean up any duplicate slashes that might have been created
return joined.replace(/\/+/g, "/");
};