export function isValidUrl(string) {
  if (typeof string === 'number') {
    throw new Error(`URL: ${string} is not valid because it's a number.`);
  }

  if (!string) {
    throw new Error(
      "The provided URL is not valid because it's empty or undefined.",
    );
  }

  string = string.toLowerCase(); // convert to lowercase
  const res = string.match(
    '^(https?:\\/\\/)?' + // protocol
      '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name and extension
      '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
      '(\\:\\d+)?' + // port
      '(\\/[-a-z\\d%@_.~+&:]*)*' + // path
      '(\\?[;&a-z\\d%@_.,~+&:=()!@#-]*)?' + // query string
      '(\\#[-a-z\\d_\\/]*)?$',
    'i',
  ); // fragment locator
  if (res === null) {
    throw new Error(
      `Invalid URL: ${string} Please check it for any typos or missing parts.`,
    );
  }
  return true;
}
