is-domain-loose.js 626 B

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