domain-deduper.js 807 B

12345678910111213141516171819202122232425262728293031
  1. // @ts-check
  2. const createTrie = require('./trie');
  3. /**
  4. * @param {string[]} inputDomains
  5. */
  6. const domainDeduper = (inputDomains) => {
  7. const trie = createTrie(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. // eslint-disable-next-line sukka/unicorn/no-array-method-this-argument -- it is not an array
  16. trie.find(d, false).forEach(f => sets.delete(f));
  17. // if `.example.com` exists, then `example.com` should also be removed
  18. const a = d.slice(1);
  19. if (trie.has(a)) {
  20. sets.delete(a);
  21. }
  22. }
  23. return Array.from(sets);
  24. };
  25. module.exports.domainDeduper = domainDeduper;