domain-deduper.ts 533 B

12345678910111213141516171819202122232425
  1. import { createTrie } from './trie';
  2. export const domainDeduper = (inputDomains: string[]): string[] => {
  3. const trie = createTrie(inputDomains);
  4. const sets = new Set(inputDomains);
  5. for (let j = 0, len = inputDomains.length; j < len; j++) {
  6. const d = inputDomains[j];
  7. if (d[0] !== '.') {
  8. continue;
  9. }
  10. trie.find(d, false).forEach(f => sets.delete(f));
  11. const a: string = d.slice(1);
  12. if (sets.has(a)) {
  13. sets.delete(a);
  14. }
  15. }
  16. return Array.from(sets);
  17. };
  18. export default domainDeduper;