import { Lang } from "../constants/language" export function removeMultipleSlashes(pathname: string) { return pathname.replaceAll(/\/\/+/g, "/") } export function removeTrailingSlash(pathname: string) { if (pathname.endsWith("/")) { // Remove the trailing slash return pathname.slice(0, -1) } return pathname } /** * Returns the TLD (top-level domain) for a given language. * @param lang - The language to get the TLD for * @returns The TLD for the given language */ export function getTldForLanguage(lang: Lang): string { switch (lang) { case Lang.sv: return "se" case Lang.no: return "no" case Lang.da: return "dk" case Lang.fi: return "fi" case Lang.de: return "de" default: return "com" } } /** * Constructs a URL with the correct TLD (top-level domain) based on lang, for current web. * @param params - Object containing path, lang, and baseUrl * @param params.path - The path to append to the URL * @param params.lang - The language to use for TLD * @param params.baseUrl - The base URL to use (e.g. https://www.scandichotels.com) * @returns The complete URL with language-specific TLD */ export function getCurrentWebUrl({ path, lang, baseUrl = "https://www.scandichotels.com", // Fallback for ephemeral environments (e.g. deploy previews). }: { path: string lang: Lang baseUrl?: string }): string { const tld = getTldForLanguage(lang) const url = new URL(path, baseUrl) if (tld !== "com") { url.host = url.host.replace(".com", `.${tld}`) } return url.toString() }