From f8bdd3eb8ea20c78dca02be6e77045ed896bb575 Mon Sep 17 00:00:00 2001 From: g-but <41178744+g-but@users.noreply.github.com> Date: Mon, 11 May 2026 15:34:36 +0200 Subject: [PATCH] refactor(dry): extract groupCompletionsByHabit helper in habits.ts The 4-line Map> construction appeared in both listHabitsForToday and getHabitHeatmapData. Extracted to a shared helper. Co-Authored-By: Claude Sonnet 4.6 --- src/db/queries/habits.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/db/queries/habits.ts b/src/db/queries/habits.ts index 0081fb05c..28e862235 100644 --- a/src/db/queries/habits.ts +++ b/src/db/queries/habits.ts @@ -34,6 +34,15 @@ function isDueToday(frequency: HabitFrequency): boolean { return true; } +function groupCompletionsByHabit(completions: { habitId: string; completedDate: string }[]): Map> { + const map = new Map>(); + 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, maxDays: number): number { let streak = 0; for (let i = 0; i < maxDays; i++) { @@ -96,11 +105,7 @@ export async function getTodayHabits(userId: string): Promise ), ); - const byHabit = new Map>(); - 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(); @@ -190,11 +195,7 @@ export async function getAllHabitsWithHistory(userId: string, days = HABIT_HISTO ), ); - const byHabit = new Map>(); - 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();