Skip to content

Commit

Permalink
fix pattern matching (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
idaho authored Oct 9, 2024
1 parent 6038369 commit 5dc999a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
12 changes: 3 additions & 9 deletions src/utils/eventsToItems.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { filterEventByPatterns } from './filterEventByPatterns';

import type { CalendarEvent } from './calendarEvents';
import type { CalendarItem } from './calendarItem';
import type { ItemSettings } from './itemSettings';
Expand Down Expand Up @@ -31,20 +33,12 @@ const eventToItem = (event: CalendarEvent | undefined, { pattern, useSummary }:
return [];
}

const { content: { summary }} = event;

const possibleTypes = pattern.
map((pat, idx) => ({
...pat,
idx
})).
filter((pat: Pattern) => {
if (pat.pattern_exact) {
return pat.pattern && summary.toLowerCase() === pat.pattern.toLowerCase();
}

return pat.pattern && summary.toLowerCase().includes(pat.pattern.toLowerCase());
});
filter(pat => filterEventByPatterns(pat, event));

if (possibleTypes.length > 0) {
return possibleTypes.map(pat => getData(event, pat, useSummary));
Expand Down
14 changes: 14 additions & 0 deletions src/utils/filterEventByPatterns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { CalendarEvent } from './calendarEvents';
import type { ItemSettings } from './itemSettings';

const filterEventByPatterns = ({ pattern, pattern_exact }: ItemSettings, { content: { summary }}: CalendarEvent) => {
if (pattern_exact) {
return pattern && summary.toLowerCase() === pattern.toLowerCase();
}

return pattern && summary.toLowerCase().includes(pattern.toLowerCase());
};

export {
filterEventByPatterns
};
5 changes: 3 additions & 2 deletions src/utils/findActiveEvents.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getDayFromDate } from './getDayFromDate';
import { getTimeZoneOffset } from './getTimeZoneOffset';
import { filterEventByPatterns } from './filterEventByPatterns';

import type { CalendarEvent } from './calendarEvents';
import type { TrashCardConfig } from '../cards/trash-card/trash-card-config';
Expand All @@ -26,9 +27,9 @@ const isMatchingAnyPatterns = (item: CalendarEvent, config: Config) => {
}

const trashTypes = config.pattern.filter(pat => pat.type !== 'others');
const patterns = trashTypes.map(pat => pat.pattern).filter(pattern => pattern !== undefined);
const patterns = trashTypes.filter(pattern => pattern.pattern !== undefined);

return patterns.length === 0 || patterns.some(pattern => item.content.summary.toLowerCase().includes(pattern.toLowerCase()));
return patterns.length === 0 || patterns.some(pat => filterEventByPatterns(pat, item));
};

const isNotPastWholeDayEvent = (item: CalendarEvent, now: Date, dropAfter: boolean): boolean =>
Expand Down

0 comments on commit 5dc999a

Please sign in to comment.