Surge_by_SukkaW/Build/lib/domain-deduper.js
2023-09-18 22:44:43 +08:00

31 lines
709 B
JavaScript

// @ts-check
const createTrie = require('./trie');
/**
* @param {string[]} inputDomains
*/
const domainDeduper = (inputDomains) => {
const trie = createTrie(inputDomains);
const sets = new Set(inputDomains);
for (let j = 0, len = inputDomains.length; j < len; j++) {
const d = inputDomains[j];
if (d[0] !== '.') {
continue;
}
// delete all included subdomains (ends with `.example.com`)
trie.find(d, false).forEach(f => sets.delete(f));
// if `.example.com` exists, then `example.com` should also be removed
const a = d.slice(1);
if (trie.has(a)) {
sets.delete(a);
}
}
return Array.from(sets);
};
module.exports.domainDeduper = domainDeduper;