// Copyright 2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only export function maybeParseUrl(value: string): undefined | URL { if (typeof value === 'string') { try { return new URL(value); } catch (err) { /* Errors are ignored. */ } } return undefined; } export function setUrlSearchParams( url: Readonly, searchParams: Readonly> ): URL { const result = cloneUrl(url); result.search = ''; for (const [key, value] of Object.entries(searchParams)) { if (value == null) { continue; } result.searchParams.append(key, String(value)); } return result; } function cloneUrl(url: Readonly): URL { return new URL(url.href); } export function urlPathFromComponents( components: ReadonlyArray ): string { return `/${components.filter(Boolean).map(encodeURIComponent).join('/')}`; }