src/lib/hostname.js
function getHostname(url, userOptions = {}) {
  const defaults = {
    removeSubdomains: true,
  };
  const options = Object.assign({}, defaults, userOptions);
  const domainRegExp = /^(?:[a-z]+:\/\/)?(?:[^/@]+@)?([^/:]+)/i;
  const ipAddressRegExp = /^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/;
  const domainMatch = url.match(domainRegExp);

  if (domainMatch === null) {
    throw new Error(`URL is invalid: ${url}`);
  }

  // If the hostname is an IP address, no further processing can be done.
  const hostname = domainMatch[1];
  if (ipAddressRegExp.test(hostname)) {
    return hostname;
  }

  // Return the hostname with subdomains removed, if requested.
  return (options.removeSubdomains) ? removeSubdomains(hostname) : hostname;
}
function getHostname(url, userOptions = {}) {
  const defaults = {
    removeSubdomains: true,
  };
  const options = Object.assign({}, defaults, userOptions);
  const domainRegExp = /^(?:[a-z]+:\/\/)?(?:[^/@]+@)?([^/:]+)/i;
  const ipAddressRegExp = /^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/;
  const domainMatch = url.match(domainRegExp);

  if (domainMatch === null) {
    throw new Error(`URL is invalid: ${url}`);
  }

  // If the hostname is an IP address, no further processing can be done.
  const hostname = domainMatch[1];
  if (ipAddressRegExp.test(hostname)) {
    return hostname;
  }

  // Return the hostname with subdomains removed, if requested.
  return (options.removeSubdomains) ? removeSubdomains(hostname) : hostname;
}
function getHostname ( url , userOptions = { } ) { const defaults = { removeSubdomains : true , } ; const options = Object . assign ( { } , defaults , userOptions ) ; const domainRegExp = / ^(?:[a-z]+:\/\/)?(?:[^/@]+@)?([^/:]+) / i ; const ipAddressRegExp = / ^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$ / ; const domainMatch = url . match ( domainRegExp ) ; if ( domainMatch === null ) { throw new Error ( ` ${ url } ` ) ; } // If the hostname is an IP address, no further processing can be done. const hostname = domainMatch [ 1 ] ; if ( ipAddressRegExp . test ( hostname ) ) { return hostname ; } // Return the hostname with subdomains removed, if requested. return ( options . removeSubdomains ) ? removeSubdomains ( hostname ) : hostname ; }
Isolate the domain name of a URL.
Isolate the domain name of a URL .