domain-deduper.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { createTrie, type Trie } from './trie';
  2. export function domainDeduper(inputDomains: string[] | Trie, toArray?: true): string[];
  3. export function domainDeduper(inputDomains: string[] | Trie, toArray: false): Set<string>;
  4. export function domainDeduper(inputDomains: string[] | Trie, toArray = true): string[] | Set<string> {
  5. let trie: Trie;
  6. if (Array.isArray(inputDomains)) {
  7. trie = createTrie(inputDomains, true, true);
  8. } else if (!inputDomains.hostnameMode || !inputDomains.smolTree) {
  9. throw new Error('Invalid trie');
  10. } else {
  11. trie = inputDomains;
  12. }
  13. const dumped = trie.dump();
  14. if (toArray) {
  15. return dumped;
  16. }
  17. return new Set(dumped);
  18. // const trie = createTrie(inputDomains, true);
  19. // const sets = new Set(inputDomains);
  20. // for (let i = 0, len1 = inputDomains.length; i < len1; i++) {
  21. // const d = inputDomains[i];
  22. // if (d[0] !== '.') {
  23. // continue;
  24. // }
  25. // trie.substractSetInPlaceFromFound(d, sets);
  26. // sets.delete(d.slice(1));
  27. // }
  28. // if (toArray) {
  29. // return Array.from(sets);
  30. // }
  31. // return sets;
  32. }