-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathurlUtils.ts
More file actions
164 lines (145 loc) · 4.13 KB
/
Copy pathurlUtils.ts
File metadata and controls
164 lines (145 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Determines whether a given string is a valid URL.
*
* @param url - The string to validate as a URL.
* @returns `true` if the string is a valid URL, `false` otherwise.
*/
export const isValidUrl = (url: string): boolean => {
try {
new URL(url);
return true;
} catch {
return false;
}
};
/**
* Checks if a given string is a valid domain name.
*
* @param domain - The domain name string to validate.
* @returns `true` if the domain is valid, otherwise `false`.
*/
export const isValidDomain = (domain: string): boolean => {
const domainRegex =
/^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
return domainRegex.test(domain);
};
/**
* Normalizes a given input string into a valid URL or a Google search URL.
*
* @param input - The string to normalize as a URL or search query.
* @returns The normalized URL or search URL.
*/
export const normalizeUrl = (input: string): string => {
if (!input.trim()) {
return "";
}
const trimmedInput = input.trim();
// If it looks like a search query (contains spaces or no dots), use Google search
if (
trimmedInput.includes(" ") ||
(!trimmedInput.includes(".") && !trimmedInput.startsWith("localhost"))
) {
return `https://www.google.com/search?q=${encodeURIComponent(
trimmedInput
)}`;
}
// Handle localhost
if (trimmedInput.startsWith("localhost")) {
return trimmedInput.startsWith("http")
? trimmedInput
: `http://${trimmedInput}`;
}
// Add protocol if missing
if (
!trimmedInput.startsWith("http://") &&
!trimmedInput.startsWith("https://")
) {
return `https://${trimmedInput}`;
}
return trimmedInput;
};
export const extractDomain = (url: string): string => {
try {
return new URL(url).hostname;
} catch {
return url;
}
};
export const extractProtocol = (url: string): string => {
try {
return new URL(url).protocol;
} catch {
return "https:";
}
};
/**
* Determines if a given URL is considered secure.
*
* @param url - The URL string to check.
* @returns `true` if the URL is secure, otherwise `false`.
*/
export const isSecureUrl = (url: string): boolean => {
return url.startsWith("https://") || url.startsWith("localhost");
};
/**
* Formats a given URL string for display by extracting the hostname,
* pathname (if not root), and search parameters.
* If the input is not a valid URL, returns the original string.
*
* @param url - The URL string to format for display.
* @returns A formatted string suitable for display, or the original string if invalid.
*/
export const formatUrlForDisplay = (url: string): string => {
try {
const urlObj = new URL(url);
let displayUrl = urlObj.hostname;
if (urlObj.pathname !== "/") {
displayUrl += urlObj.pathname;
}
if (urlObj.search) {
displayUrl += urlObj.search;
}
return displayUrl;
} catch {
return url;
}
};
export const getUrlWithoutProtocol = (url: string): string => {
try {
const urlObj = new URL(url);
return url.replace(`${urlObj.protocol}//`, "");
} catch {
return url;
}
};
export const isSameOrigin = (url1: string, url2: string): boolean => {
try {
const urlObj1 = new URL(url1);
const urlObj2 = new URL(url2);
return urlObj1.origin === urlObj2.origin;
} catch {
return false;
}
};
/**
* Builds a search URL for the specified search engine using the provided query.
*
* @param query - The search query string to be encoded and appended to the URL.
* @param searchEngine - The search engine to use ("google", "bing", or "duckduckgo"). Defaults to "google".
* @returns The complete search URL for the specified search engine.
*/
export const buildSearchUrl = (
query: string,
searchEngine: string = "google"
): string => {
const encodedQuery = encodeURIComponent(query);
const searchEngines = {
google: `https://www.google.com/search?q=${encodedQuery}`,
bing: `https://www.bing.com/search?q=${encodedQuery}`,
duckduckgo: `https://duckduckgo.com/?q=${encodedQuery}`,
};
return (
searchEngines[searchEngine as keyof typeof searchEngines] ||
searchEngines.google
);
};