function parseUrl(input, safe = true) {
  let url = input;

  if (safe) {
    const parser = document.createElement('a');
    parser.href = url;
    url = parser.href;
  }

  try {
    return new URL(url);
  } catch (_) {
    return null;
  }
}
Parse a string to a URL object
@param {String} input - the URL to be parsed
@param {Boolean} safe - failsafe parsing