safeURL.tsx 327 B

1234567891011
  1. /**
  2. * Does not throw error on invalid input and returns the parsed URL object
  3. * if the input is a valid URL, otherwise returns undefined.
  4. */
  5. export function safeURL(input: string | URL, base?: string | undefined): URL | undefined {
  6. try {
  7. return new globalThis.URL(input, base);
  8. } catch {
  9. return undefined;
  10. }
  11. }