Skip to content

Commit 8f94a6c

Browse files
📝 Add docstrings to event-limit
Docstrings generation was requested by @skateman. * #48 (comment) The following files were modified: * `src/functions/calendar.ts`
1 parent e894332 commit 8f94a6c

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/functions/calendar.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,23 @@ function transformEvents(
6565
return events.map((event) => new CalendarEvent(event, entity, config));
6666
}
6767

68+
/**
69+
* Filter calendar events based on display configuration and apply the configured event limit.
70+
*
71+
* Filters out events that:
72+
* - are all-day and end before the start of the day plus `config.advance` days,
73+
* - are all-day when `config.show_all_day_events` is false,
74+
* - are in the past when `config.show_past_events` is false.
75+
*
76+
* @param events - The list of calendar events to filter.
77+
* @param config - Card configuration; uses `advance`, `show_all_day_events`, `show_past_events`, and `event_limit`.
78+
* @returns The filtered events limited to the first `config.event_limit` entries. If `config.event_limit` is 0, the original `events` array is returned unchanged.
79+
*/
6880
function filterEvents(
6981
events: CalendarEvent[],
7082
config: CardConfig,
7183
): CalendarEvent[] {
72-
return events.filter((event: CalendarEvent): boolean => {
84+
const filteredEvents = events.filter((event: CalendarEvent): boolean => {
7385
if (
7486
event.isAllDay
7587
&& event.end.isBefore(
@@ -91,8 +103,16 @@ function filterEvents(
91103

92104
return true;
93105
});
106+
107+
return (config.event_limit === 0) ? events : events.slice(0, config.event_limit);
94108
}
95109

110+
/**
111+
* Compute the Unix timestamp to use when comparing an event's start time.
112+
*
113+
* @param event - The CalendarEvent to evaluate; for multi-day events that are not on their first day, use the start of today as the comparison point.
114+
* @returns The Unix timestamp (seconds since epoch) to use as the event's comparison start: the start of today for applicable multi-day events, otherwise the event's actual start time.
115+
*/
96116
function getCompareStart(event: CalendarEvent): number {
97117
if (event.isMultiDay && !event.isFirstDay) {
98118
return dayjs().startOf("day").unix();

0 commit comments

Comments
 (0)