is-domain-loose.js 448 B

123456789101112131415161718192021222324
  1. // @ts-check
  2. const tldts = require('./cached-tld-parse');
  3. /**
  4. * @param {string | null | undefined} domain
  5. */
  6. module.exports.normalizeDomain = (domain) => {
  7. if (!domain) {
  8. return null;
  9. }
  10. const { isIcann, isPrivate, hostname, isIp } = tldts.parse(domain);
  11. if (isIp) {
  12. return null;
  13. }
  14. if (isIcann || isPrivate) {
  15. if (hostname?.[0] === '.') {
  16. return hostname.slice(1);
  17. }
  18. return hostname;
  19. }
  20. return null;
  21. };