domain-deduper.js 702 B

123456789101112131415161718192021222324252627282930
  1. // @ts-check
  2. const Trie = require('./trie');
  3. /**
  4. * @param {string[]} inputDomains
  5. */
  6. const domainDeduper = (inputDomains) => {
  7. const trie = Trie.from(inputDomains);
  8. const sets = new Set(inputDomains);
  9. for (let j = 0, len = inputDomains.length; j < len; j++) {
  10. const d = inputDomains[j];
  11. if (d[0] !== '.') {
  12. continue;
  13. }
  14. // delete all included subdomains (ends with `.example.com`)
  15. trie.find(d, false).forEach(f => sets.delete(f));
  16. // if `.example.com` exists, then `example.com` should also be removed
  17. const a = d.slice(1);
  18. if (trie.has(a)) {
  19. sets.delete(a);
  20. }
  21. }
  22. return Array.from(sets);
  23. };
  24. module.exports.domainDeduper = domainDeduper;