🐛 fix(subscribe): Extract Domain

This commit is contained in:
web@ppanel
2024-12-05 20:52:45 +07:00
parent a451f44c81
commit 40d61a9843
5 changed files with 17 additions and 18 deletions
+13 -11
View File
@@ -7,7 +7,7 @@ export * from './unit-conversions';
export const isBrowser = () => typeof window !== 'undefined';
export function getNextResetDate(startDate: Date | number) {
let time = new Date(startDate);
const time = new Date(startDate);
const resetDay = time.getDate();
const currentDate = new Date();
if (isNaN(time.getTime())) {
@@ -27,21 +27,23 @@ export function getNextResetDate(startDate: Date | number) {
}
}
export function extractDomain(url: string): string | null {
/**
* Extracts the full domain or root domain from a URL.
*
* @param url - The URL to extract the domain from.
* @param extractRoot - If true, extracts the root domain (e.g., example.com). If false, extracts the full domain (e.g., sub.example.com).
* @returns The extracted domain or root domain, or null if the URL is invalid.
*/
export function extractDomain(url: string, extractRoot = true): string | null {
try {
const hostname = new URL(url).hostname;
if (hostname.match(/^\d{1,3}(\.\d{1,3}){3}$/)) {
const { hostname } = new URL(url);
if (/^\d{1,3}(\.\d{1,3}){3}$/.test(hostname)) {
return hostname;
}
const domainParts = hostname.split('.').filter(Boolean);
if (domainParts.length >= 2) {
const topLevelDomain = domainParts.slice(-2).join('.');
return topLevelDomain;
if (extractRoot && domainParts.length > 2) {
return domainParts.slice(-2).join('.');
}
return hostname;
} catch (error) {
console.error('Invalid URL:', error);