Skip to content

Commit 28873a8

Browse files
committed
fix: clamp weekEnd
1 parent 80bdbaa commit 28873a8

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

app/composables/useCharts.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@ function buildDailyEvolutionFromDaily(
134134
function buildRollingWeeklyEvolutionFromDaily(
135135
daily: Array<{ day: string; downloads: number }>,
136136
rangeStartIso: string,
137+
rangeEndIso: string,
137138
): WeeklyDownloadPoint[] {
138139
const sorted = daily.slice().sort((a, b) => a.day.localeCompare(b.day))
139140
const rangeStartDate = parseIsoDateOnly(rangeStartIso)
141+
const rangeEndDate = parseIsoDateOnly(rangeEndIso)
140142

141143
const groupedByIndex = new Map<number, number>()
142144

@@ -155,8 +157,12 @@ function buildRollingWeeklyEvolutionFromDaily(
155157
const weekStartDate = addDays(rangeStartDate, weekIndex * 7)
156158
const weekEndDate = addDays(weekStartDate, 6)
157159

160+
// Clamp weekEnd to the actual data range end date
161+
const clampedWeekEndDate =
162+
weekEndDate.getTime() > rangeEndDate.getTime() ? rangeEndDate : weekEndDate
163+
158164
const weekStartIso = toIsoDateString(weekStartDate)
159-
const weekEndIso = toIsoDateString(weekEndDate)
165+
const weekEndIso = toIsoDateString(clampedWeekEndDate)
160166

161167
return {
162168
downloads,
@@ -350,7 +356,7 @@ export function useCharts() {
350356

351357
if (resolvedOptions.granularity === 'day') return buildDailyEvolutionFromDaily(sortedDaily)
352358
if (resolvedOptions.granularity === 'week')
353-
return buildRollingWeeklyEvolutionFromDaily(sortedDaily, startIso)
359+
return buildRollingWeeklyEvolutionFromDaily(sortedDaily, startIso, endIso)
354360
if (resolvedOptions.granularity === 'month') return buildMonthlyEvolutionFromDaily(sortedDaily)
355361
return buildYearlyEvolutionFromDaily(sortedDaily)
356362
}

0 commit comments

Comments
 (0)