You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

localHostOrDomainIs.js 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * Module exports.
  3. */
  4. module.exports = localHostOrDomainIs;
  5. /**
  6. * Is true if the hostname matches exactly the specified hostname, or if there is
  7. * no domain name part in the hostname, but the unqualified hostname matches.
  8. *
  9. * Examples:
  10. *
  11. * ``` js
  12. * localHostOrDomainIs("www.netscape.com", "www.netscape.com")
  13. * // is true (exact match).
  14. *
  15. * localHostOrDomainIs("www", "www.netscape.com")
  16. * // is true (hostname match, domain not specified).
  17. *
  18. * localHostOrDomainIs("www.mcom.com", "www.netscape.com")
  19. * // is false (domain name mismatch).
  20. *
  21. * localHostOrDomainIs("home.netscape.com", "www.netscape.com")
  22. * // is false (hostname mismatch).
  23. * ```
  24. *
  25. * @param {String} host the hostname from the URL.
  26. * @param {String} hostdom fully qualified hostname to match against.
  27. * @return {Boolean}
  28. */
  29. function localHostOrDomainIs (host, hostdom) {
  30. var parts = String(host).split('.');
  31. var domparts = String(hostdom).split('.');
  32. var matches = true;
  33. for (var i = 0; i < parts.length; i++) {
  34. if (parts[i] !== domparts[i]) {
  35. matches = false;
  36. break;
  37. }
  38. }
  39. return matches;
  40. }