Streak Calculator
The streak calculator tracks consecutive running days and maintains streak history.
How Streaks Work
A streak is a sequence of consecutive days where at least one run was recorded.
Rules
- Multiple runs on the same day count as one day
- A streak continues as long as you run at least once per day
- Missing a day ends the current streak
- A new streak starts with the next run
Streak Types
Current Streak
The number of consecutive days ending with today (or yesterday if today has no runs yet).
typescript
function getCurrentStreak(dailyStats: DailyStats[]): number {
// Sort by date descending
// Count consecutive days from most recent
}Longest Streak
The longest consecutive run sequence ever recorded.
Streak History
All past streaks with start date, end date, and length.
API Response
json
{
"streak": {
"current": 5,
"longest": 21,
"history": [
{
"start": "2024-01-10",
"end": "2024-01-30",
"length": 21
},
{
"start": "2024-02-05",
"end": "2024-02-09",
"length": 5
}
]
}
}Implementation
The streak calculator is implemented in packages/business-logic/src/streak-calculator.ts.
typescript
import { calculateStreaks } from '@running-days/business-logic';
const streakData = calculateStreaks(dailyStats);
console.log(`Current streak: ${streakData.current} days`);Edge Cases
Timezone Handling
Streaks are calculated in the userβs local timezone. A run at 11:59 PM counts for that day, not the next.
Same-Day Multiple Runs
If you run in the morning and evening, both contribute to daily stats, but you only get credit for one running day.
Future Dates
Runs with future dates are ignored in streak calculations.