refactor(dry): extract groupCompletionsByHabit helper in habits.ts

The 4-line Map<habitId, Set<completedDate>> construction appeared in both
listHabitsForToday and getHabitHeatmapData. Extracted to a shared helper.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
g-butandClaude Sonnet 4.6 committed 2026-05-11 15:34:36 +02:00
1 parent 1804adb9b3
commit f8bdd3eb8e
1 file changed
+11 -10
+11 -10
View File
@@ -34,6 +34,15 @@ function isDueToday(frequency: HabitFrequency): boolean {
return true;
}
function groupCompletionsByHabit(completions: { habitId: string; completedDate: string }[]): Map<string, Set<string>> {
const map = new Map<string, Set<string>>();
for (const c of completions) {
if (!map.has(c.habitId)) map.set(c.habitId, new Set());
map.get(c.habitId)!.add(c.completedDate);
}
return map;
}
function computeStreak(dates: Set<string>, maxDays: number): number {
let streak = 0;
for (let i = 0; i < maxDays; i++) {
@@ -96,11 +105,7 @@ export async function getTodayHabits(userId: string): Promise<HabitWithStatus[]>
),
);
const byHabit = new Map<string, Set<string>>();
for (const c of completions) {
if (!byHabit.has(c.habitId)) byHabit.set(c.habitId, new Set());
byHabit.get(c.habitId)!.add(c.completedDate);
}
const byHabit = groupCompletionsByHabit(completions);
return dueHabits.map((h) => {
const dates = byHabit.get(h.id) ?? new Set<string>();
@@ -190,11 +195,7 @@ export async function getAllHabitsWithHistory(userId: string, days = HABIT_HISTO
),
);
const byHabit = new Map<string, Set<string>>();
for (const c of completions) {
if (!byHabit.has(c.habitId)) byHabit.set(c.habitId, new Set());
byHabit.get(c.habitId)!.add(c.completedDate);
}
const byHabit = groupCompletionsByHabit(completions);
return allHabits.map((h) => {
const dates = byHabit.get(h.id) ?? new Set<string>();