UNPKG

983 BJavaScriptView Raw
1const urlPattern = /^[a-z][*+.a-z-]+:\/\//i;
2export const NO_HOSTNAME = Symbol("NO_HOSTNAME");
3export const fromUrl = (urlLike) => {
4 /* istanbul ignore next */
5 if (typeof URL !== "function") {
6 throw new Error("Looks like the new URL() constructor is not globally available in your environment. Please make sure to use a polyfill.");
7 }
8 // Extra check for non-TypeScript users
9 if (typeof urlLike !== "string") {
10 return NO_HOSTNAME;
11 }
12 // URLs that start with // are protocol relative
13 const url = urlLike.startsWith("//")
14 ? `http:${urlLike}`
15 : // URLs that start with / do not have a hostname section
16 urlLike.startsWith("/")
17 ? urlLike
18 : urlPattern.test(urlLike)
19 ? urlLike
20 : `http://${urlLike}`;
21 try {
22 return new URL(url).hostname;
23 }
24 catch (_a) {
25 return NO_HOSTNAME;
26 }
27};
28//# sourceMappingURL=from-url.js.map
\No newline at end of file