[WEB-4881] feat(utils): add URL display formatter #7770

This commit is contained in:
Vihar Kurama 2025-09-29 10:03:25 +01:00 committed by GitHub
parent bedc1fae1f
commit e891482a97
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -97,6 +97,24 @@ export function extractHostname(url: string): string {
return hostname;
}
/**
* Returns a readable representation of a URL by stripping the protocol
* and any trailing slash. For valid URLs, only the host is returned.
* Invalid URLs are sanitized by removing the protocol and trailing slash.
*
* @param url - The URL string to format
* @returns The formatted domain for display
*/
export function formatURLForDisplay(url: string): string {
if (!url) return "";
try {
return new URL(url).host;
} catch (_error) {
return extractHostname(url);
}
}
/**
* Extracts and validates the TLD (Top Level Domain) from a URL string.
*