Skip to content

Commit e891482

Browse files
authored
[WEB-4881] feat(utils): add URL display formatter #7770
1 parent bedc1fa commit e891482

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

packages/utils/src/url.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,24 @@ export function extractHostname(url: string): string {
9797
return hostname;
9898
}
9999

100+
/**
101+
* Returns a readable representation of a URL by stripping the protocol
102+
* and any trailing slash. For valid URLs, only the host is returned.
103+
* Invalid URLs are sanitized by removing the protocol and trailing slash.
104+
*
105+
* @param url - The URL string to format
106+
* @returns The formatted domain for display
107+
*/
108+
export function formatURLForDisplay(url: string): string {
109+
if (!url) return "";
110+
111+
try {
112+
return new URL(url).host;
113+
} catch (_error) {
114+
return extractHostname(url);
115+
}
116+
}
117+
100118
/**
101119
* Extracts and validates the TLD (Top Level Domain) from a URL string.
102120
*

0 commit comments

Comments
 (0)