From c6b20e68f6dc94738240ae349c16c14ebd5f9f50 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 9 Sep 2023 16:24:53 -0500 Subject: [PATCH] db/events: refactor filterIndexableTags function --- src/db/events.ts | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/db/events.ts b/src/db/events.ts index 74553d5..6dade28 100644 --- a/src/db/events.ts +++ b/src/db/events.ts @@ -194,21 +194,32 @@ async function countFilters(filters: DittoFilter[]): Promis function filterIndexableTags(event: Event, data: EventData): string[][] { const tagCounts: Record = {}; - return event.tags.reduce((results, tag) => { - const [name, value] = tag; - tagCounts[name] = (tagCounts[name] || 0) + 1; + function getCount(name: string) { + return tagCounts[name] || 0; + } - const shouldIndex = tagConditions[name]?.({ + function incrementCount(name: string) { + tagCounts[name] = getCount(name) + 1; + } + + function checkCondition(name: string, value: string, condition: TagCondition) { + return condition({ event, data, - count: tagCounts[name] - 1, + count: getCount(name), value, }); + } - if (value && value.length < 200 && shouldIndex) { + return event.tags.reduce((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); } + incrementCount(name); return results; }, []); }