domain-deduper.js 549 B

123456789101112131415161718192021222324252627
  1. const Trie = require('./trie');
  2. /**
  3. * @param {string[]} inputDomains
  4. */
  5. const domainDeduper = (inputDomains) => {
  6. const trie = Trie.from(inputDomains);
  7. const sets = new Set(inputDomains);
  8. for (let j = 0, len = inputDomains.length; j < len; j++) {
  9. const d = inputDomains[j];
  10. if (d[0] !== '.') {
  11. continue;
  12. }
  13. trie.find(d, false).forEach(f => sets.delete(f));
  14. const a = d.slice(1);
  15. if (trie.has(a)) {
  16. sets.delete(a);
  17. }
  18. }
  19. return Array.from(sets);
  20. };
  21. module.exports.domainDeduper = domainDeduper;