toot-script-condivisione-su.../src/lib/url.ts

36 lines
744 B
TypeScript
Raw Normal View History

2023-03-18 02:11:18 +01:00
/*!
2023-09-02 16:17:15 +02:00
* This file is part of ShareFedi
* https://github.com/kytta/share2fedi
*
* SPDX-FileCopyrightText: © 2023 Nikita Karamov <me@kytta.dev>
* SPDX-License-Identifier: AGPL-3.0-only
2023-03-18 02:11:18 +01:00
*/
/**
* 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;
};
2023-03-27 21:23:49 +02:00
export const getUrlDomain = (url: string | URL): string => {
if (typeof url === "string") {
url = url.trim();
if (!/^https?:\/\//.test(url)) {
url = `https://${url}`;
}
2023-03-17 23:08:15 +01:00
}
2023-03-27 21:23:49 +02:00
2023-03-17 23:08:15 +01:00
return new URL(url).host;
};