mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-16 03:00:34 +08:00
33 lines
807 B
TypeScript
33 lines
807 B
TypeScript
import { createTrie } from './trie';
|
|
|
|
export function domainDeduper(inputDomains: string[], toArray?: true): string[];
|
|
export function domainDeduper(inputDomains: string[], toArray: false): Set<string>;
|
|
export function domainDeduper(inputDomains: string[], toArray = true): string[] | Set<string> {
|
|
const trie = createTrie(inputDomains);
|
|
const sets = new Set(inputDomains);
|
|
|
|
for (let i = 0, len = inputDomains.length; i < len; i++) {
|
|
const d = inputDomains[i];
|
|
if (d[0] !== '.') {
|
|
continue;
|
|
}
|
|
|
|
const found = trie.find(d, true);
|
|
for (let j = 0, len = found.length; j < len; j++) {
|
|
sets.delete(found[j]);
|
|
}
|
|
|
|
const a: string = d.slice(1);
|
|
|
|
if (sets.has(a)) {
|
|
sets.delete(a);
|
|
}
|
|
}
|
|
|
|
if (toArray) {
|
|
return Array.from(sets);
|
|
}
|
|
|
|
return sets;
|
|
}
|