/** * Toggles a dark theme query parameter on or off for the given URL. * * @param {string} imageUrl - The URL to toggle the dark theme query parameter for. * @param {boolean} [isDarkTheme=false] - Whether to add or remove the dark theme query parameter. * * @returns {string} The modified URL with the dark theme query parameter toggled. * * @example * // Toggle the "isDarkTheme" query parameter for an image URL * const imageUrl = 'https://example.com/image.jpg' * const isDarkTheme = true * const toggledUrl = toogleImageDarkTheme(imageUrl, isDarkTheme) * console.log(toggledUrl) // Outputs: "https://example.com/image.jpg?isDarkTheme=true" */ export function toggleImageDarkTheme(imageUrl: string, isDarkTheme = false): string { try { const url = new URL(imageUrl) if (isDarkTheme) url.searchParams.set('isDarkTheme', 'true') else url.searchParams.delete('isDarkTheme') return url.href } catch (error) { return imageUrl } }