Feat: trie now supports whitelist method

This commit is contained in:
SukkaW
2024-05-26 00:53:01 +08:00
parent 05c2db6ac7
commit 48b5f609dd
2 changed files with 122 additions and 7 deletions

View File

@@ -144,8 +144,6 @@ describe('smol tree', () => {
'.blog.sub.example.com', 'sub.example.com', 'cdn.sub.example.com', '.sub.example.com'
], true, true);
console.log(trie);
expect(trie.dump()).toStrictEqual([
'.sub.example.com',
'cdn.noc.one', 'www.noc.one',
@@ -153,13 +151,11 @@ describe('smol tree', () => {
]);
});
it.only('should create simple tree - 2', () => {
it('should create simple tree - 2', () => {
const trie = createTrie([
'.skk.moe', 'blog.skk.moe', '.cdn.skk.moe', 'skk.moe'
], true, true);
console.log({ trie });
expect(trie.dump()).toStrictEqual([
'.skk.moe'
]);
@@ -170,8 +166,6 @@ describe('smol tree', () => {
'.blog.sub.example.com', 'cdn.sub.example.com', '.sub.example.com'
], true, true);
console.log(trie);
expect(trie.dump()).toStrictEqual([
'.sub.example.com'
]);
@@ -197,4 +191,54 @@ describe('smol tree', () => {
'act.commercial.shouji.360.cn'
]);
});
it('should dedupe subdomain properly', () => {
const trie = createTrie([
'skk.moe',
'anotherskk.moe',
'blog.anotherskk.moe',
'blog.skk.moe'
], true, true);
expect(trie.dump()).toStrictEqual([
'anotherskk.moe',
'blog.anotherskk.moe',
'skk.moe',
'blog.skk.moe'
]);
});
it('should efficiently whitelist domains', () => {
const trie = createTrie([
'skk.moe',
'anotherskk.moe',
'blog.anotherskk.moe',
'blog.skk.moe'
], true, true);
expect(trie.dump()).toStrictEqual([
'anotherskk.moe',
'blog.anotherskk.moe',
'skk.moe',
'blog.skk.moe'
]);
trie.whitelist('.skk.moe');
expect(trie.dump()).toStrictEqual([
'anotherskk.moe',
'blog.anotherskk.moe'
]);
trie.whitelist('anotherskk.moe');
expect(trie.dump()).toStrictEqual([
'blog.anotherskk.moe'
]);
trie.add('anotherskk.moe');
trie.whitelist('.anotherskk.moe');
expect(trie.dump()).toStrictEqual([]);
});
});