Skip to content
Merged
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
18 changes: 18 additions & 0 deletions packages/utils/src/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment thread
sriramveeraghanta marked this conversation as resolved.
*
* @param url - The URL string to format
* @returns The formatted domain for display
Comment thread
sriramveeraghanta marked this conversation as resolved.
*/
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.
*
Expand Down
Loading