normalize-domain.ts 530 B

123456789101112131415161718
  1. import * as tldts from './cached-tld-parse';
  2. import { isProbablyIpv4 } from './is-fast-ip';
  3. export const normalizeDomain = (domain: string) => {
  4. if (!domain) return null;
  5. if (isProbablyIpv4(domain)) return null;
  6. const parsed = tldts.parse2(domain);
  7. // if (parsed.isIp) return null;
  8. if (!parsed.hostname) return null;
  9. if (!parsed.isIcann && !parsed.isPrivate) return null;
  10. let h = parsed.hostname;
  11. if (h[0] === '.') h = h.slice(1);
  12. if (h.endsWith('.')) h = h.slice(0, -1);
  13. if (h) return h;
  14. return null;
  15. };