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

The 7-line streak loop appeared twice — once using HABIT_HISTORY_DAYS
as the bound (listHabitsForToday) and once using a dynamic days param
(getHabitHeatmapData). Extracted to a private computeStreak(dates, maxDays)
function eliminating the duplication.

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:32:30 +02:00
1 parent 4a49716f79
commit 1804adb9b3
1 file changed
+13 -14
+13 -14
View File
@@ -34,6 +34,17 @@ function isDueToday(frequency: HabitFrequency): boolean {
return true;
}
function computeStreak(dates: Set<string>, maxDays: number): number {
let streak = 0;
for (let i = 0; i < maxDays; i++) {
const d = new Date();
d.setDate(d.getDate() - i);
if (dates.has(toLocalDateStr(d))) streak++;
else break;
}
return streak;
}
export function scheduledDays(frequency: HabitFrequency, days: number): number {
let count = 0;
for (let i = 0; i < days; i++) {
@@ -94,13 +105,7 @@ export async function getTodayHabits(userId: string): Promise<HabitWithStatus[]>
return dueHabits.map((h) => {
const dates = byHabit.get(h.id) ?? new Set<string>();
const doneToday = dates.has(today);
let streak = 0;
for (let i = 0; i < HABIT_HISTORY_DAYS; i++) {
const d = new Date();
d.setDate(d.getDate() - i);
if (dates.has(toLocalDateStr(d))) streak++;
else break;
}
const streak = computeStreak(dates, HABIT_HISTORY_DAYS);
return { id: h.id, title: h.title, frequency: h.frequency, sortOrder: h.sortOrder, doneToday, streak };
});
}
@@ -193,13 +198,7 @@ export async function getAllHabitsWithHistory(userId: string, days = HABIT_HISTO
return allHabits.map((h) => {
const dates = byHabit.get(h.id) ?? new Set<string>();
let streak = 0;
for (let i = 0; i < days; i++) {
const d = new Date();
d.setDate(d.getDate() - i);
if (dates.has(toLocalDateStr(d))) streak++;
else break;
}
const streak = computeStreak(dates, days);
return {
id: h.id,
title: h.title,