|
5 | 5 | /** |
6 | 6 | * Adds the appropriate protocol (http or https) to the given URL |
7 | 7 | * based on whether the URL contains the string 'localhost'. |
8 | | - * @param {string} url - The URL to modify. |
9 | | - * @returns {string} A new URL string with the appropriate protocol added. |
| 8 | + * @param url - The URL to modify. |
| 9 | + * @returns A new URL string with the appropriate protocol added. |
10 | 10 | */ |
11 | 11 | export const addHttp = (url?: string): string => { |
12 | 12 | return url ? `${url.includes("localhost") ? "http" : "https"}://${url}` : "" |
13 | 13 | } |
14 | 14 |
|
15 | 15 | /** |
16 | 16 | * Removes the protocol from the given URL string. |
17 | | - * @param {string} url - The URL string to modify. |
18 | | - * @returns {string} A new URL string with the protocol removed. |
| 17 | + * @param url - The URL string to modify. |
| 18 | + * @returns A new URL string with the protocol removed. |
19 | 19 | */ |
20 | 20 | export const removeHttp = (url?: string): string => { |
21 | 21 | return url ? url.replace(/^https?:\/\//, "") : "" |
22 | 22 | } |
23 | 23 |
|
24 | 24 | /** |
25 | 25 | * Removes the trailing slash from the given URL string. |
26 | | - * @param {string} url - The URL string to modify. |
27 | | - * @returns {string} A new URL string with the trailing slash removed. |
| 26 | + * @param url - The URL string to modify. |
| 27 | + * @returns A new URL string with the trailing slash removed. |
28 | 28 | */ |
29 | 29 | export const removeTrailingSlash = (url?: string): string => { |
30 | 30 | return url ? url.replace(/\/$/, "") : "" |
31 | 31 | } |
32 | 32 |
|
33 | 33 | /** |
34 | 34 | * Checks if the given URL is an external link. |
35 | | - * @param {string} url - The URL to check. |
36 | | - * @returns {boolean} A boolean indicating whether the URL is an external link. |
| 35 | + * @param url - The URL to check. |
| 36 | + * @returns A boolean indicating whether the URL is an external link. |
37 | 37 | */ |
38 | 38 | export const isExternalLink = (url?: string): boolean => { |
39 | 39 | return url ? url.includes("http") : false |
40 | 40 | } |
| 41 | + |
| 42 | +/** |
| 43 | + * Strips the subpath from a URL, returning only the protocol and host. |
| 44 | + * |
| 45 | + * @param url The URL to be stripped. |
| 46 | + * @returns The URL with the subpath removed. |
| 47 | + */ |
| 48 | +export const stripURLSubpath = (url?: string) => { |
| 49 | + if (!url) return url |
| 50 | + |
| 51 | + try { |
| 52 | + const parsedUrl = new URL(url) |
| 53 | + return `${parsedUrl.protocol}//${parsedUrl.host}` |
| 54 | + } catch (error) { |
| 55 | + // If the URL is invalid, return the original string |
| 56 | + return url |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Returns the hostname of the given URL. |
| 62 | + * @param url - The URL to get the hostname from. |
| 63 | + * @returns The hostname of the URL. |
| 64 | + */ |
| 65 | +export const getUrlHostname = (url: string) => { |
| 66 | + if (isValidUrl(url)) { |
| 67 | + return new URL(url).hostname |
| 68 | + } |
| 69 | + |
| 70 | + return url |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Checks if the given URL is valid. |
| 75 | + * @param url - The URL to check. |
| 76 | + * @returns A boolean indicating whether the URL is valid. |
| 77 | + */ |
| 78 | +export const isValidUrl = (url?: string) => { |
| 79 | + if (!url) return false |
| 80 | + |
| 81 | + try { |
| 82 | + new URL(url) |
| 83 | + return true |
| 84 | + } catch (e) { |
| 85 | + return false |
| 86 | + } |
| 87 | +} |
0 commit comments