1
0
mirror of https://github.com/NickKaramoff/toot synced 2025-06-05 21:59:33 +02:00
Files
toot-script-condivisione-su…/src/scripts/util.ts
2023-09-02 21:56:50 +02:00

33 lines
619 B
TypeScript

/*!
* © 2023 Nikita Karamov
* Licensed under AGPL v3 or later
*/
/**
* Adds missing "https://" and ending slash to the URL
*
* @param url URL to normalize
* @return normalized URL
*/
export const normalizeURL = (url: string): string => {
if (!(url.startsWith("https://") || url.startsWith("http://"))) {
url = "https://" + url;
}
if (!url.endsWith("/")) {
url += "/";
}
return url;
};
export const getUrlDomain = (url: string | URL): string => {
if (typeof url === "string") {
url = url.trim();
if (!/^https?:\/\//.test(url)) {
url = `https://${url}`;
}
}
return new URL(url).host;
};