db/events: refactor filterIndexableTags function
This commit is contained in:
parent
f8b2efb484
commit
c6b20e68f6
|
@ -194,21 +194,32 @@ async function countFilters<K extends number>(filters: DittoFilter<K>[]): Promis
|
||||||
function filterIndexableTags(event: Event, data: EventData): string[][] {
|
function filterIndexableTags(event: Event, data: EventData): string[][] {
|
||||||
const tagCounts: Record<string, number> = {};
|
const tagCounts: Record<string, number> = {};
|
||||||
|
|
||||||
return event.tags.reduce<string[][]>((results, tag) => {
|
function getCount(name: string) {
|
||||||
const [name, value] = tag;
|
return tagCounts[name] || 0;
|
||||||
tagCounts[name] = (tagCounts[name] || 0) + 1;
|
}
|
||||||
|
|
||||||
const shouldIndex = tagConditions[name]?.({
|
function incrementCount(name: string) {
|
||||||
|
tagCounts[name] = getCount(name) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkCondition(name: string, value: string, condition: TagCondition) {
|
||||||
|
return condition({
|
||||||
event,
|
event,
|
||||||
data,
|
data,
|
||||||
count: tagCounts[name] - 1,
|
count: getCount(name),
|
||||||
value,
|
value,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (value && value.length < 200 && shouldIndex) {
|
return event.tags.reduce<string[][]>((results, tag) => {
|
||||||
|
const [name, value] = tag;
|
||||||
|
const condition = tagConditions[name] as TagCondition | undefined;
|
||||||
|
|
||||||
|
if (value && condition && value.length < 200 && checkCondition(name, value, condition)) {
|
||||||
results.push(tag);
|
results.push(tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
incrementCount(name);
|
||||||
return results;
|
return results;
|
||||||
}, []);
|
}, []);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue