mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-29 01:51:52 +08:00
Chore: trie dump meta supports callback
This commit is contained in:
@@ -12,7 +12,7 @@ import { readFileByLine } from '../fetch-text-by-line';
|
|||||||
import { asyncWriteToStream } from '../async-write-to-stream';
|
import { asyncWriteToStream } from '../async-write-to-stream';
|
||||||
|
|
||||||
export abstract class RuleOutput<TPreprocessed = unknown> {
|
export abstract class RuleOutput<TPreprocessed = unknown> {
|
||||||
protected domainTrie = createTrie<unknown>(null, true);
|
protected domainTrie = createTrie<string>(null, true);
|
||||||
protected domainKeywords = new Set<string>();
|
protected domainKeywords = new Set<string>();
|
||||||
protected domainWildcard = new Set<string>();
|
protected domainWildcard = new Set<string>();
|
||||||
protected userAgent = new Set<string>();
|
protected userAgent = new Set<string>();
|
||||||
@@ -97,7 +97,7 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addDomain(domain: string) {
|
addDomain(domain: string) {
|
||||||
this.domainTrie.add(domain);
|
this.domainTrie.add(domain, domain);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,8 +109,7 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addDomainSuffix(domain: string) {
|
addDomainSuffix(domain: string) {
|
||||||
this.domainTrie.add(domain[0] === '.' ? domain : '.' + domain);
|
return this.addDomain(domain[0] === '.' ? domain : '.' + domain);
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bulkAddDomainSuffix(domains: string[]) {
|
bulkAddDomainSuffix(domains: string[]) {
|
||||||
|
|||||||
@@ -80,9 +80,7 @@ export class DomainsetOutput extends RuleOutput<Preprocessed> {
|
|||||||
)
|
)
|
||||||
.entries())
|
.entries())
|
||||||
.filter(a => a[1] > 9)
|
.filter(a => a[1] > 9)
|
||||||
.sort(
|
.sort((a, b) => (b[1] - a[1]) || a[0].localeCompare(b[0]))
|
||||||
(a, b) => (b[1] - a[1]) || a[0].localeCompare(b[0])
|
|
||||||
)
|
|
||||||
.map(([domain, count]) => `${domain}${' '.repeat(100 - domain.length)}${count}`);
|
.map(([domain, count]) => `${domain}${' '.repeat(100 - domain.length)}${count}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -316,12 +316,16 @@ abstract class Triebase<Meta = any> {
|
|||||||
return results;
|
return results;
|
||||||
};
|
};
|
||||||
|
|
||||||
public dumpMeta() {
|
public dumpMeta(onMeta: (meta: Meta) => void): void;
|
||||||
|
public dumpMeta(): Meta[];
|
||||||
|
public dumpMeta(onMeta?: (meta: Meta) => void): Meta[] | void {
|
||||||
const results: Meta[] = [];
|
const results: Meta[] = [];
|
||||||
|
|
||||||
this.walk((_suffix, meta) => {
|
const handleMeta = onMeta
|
||||||
results.push(meta);
|
? (_suffix: string[], meta: Meta) => onMeta(meta)
|
||||||
});
|
: (_suffix: string[], meta: Meta) => results.push(meta);
|
||||||
|
|
||||||
|
this.walk(handleMeta);
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user