File size: 1,886 Bytes
e603004 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import { Activity } from "../types/heatmap";
export const aggregateToWeeklyData = (dailyData: Activity[]): Activity[] => {
if (!dailyData || dailyData.length === 0) return [];
const weeklyData: Activity[] = [];
let currentWeekStart: Date | null = null;
let currentWeekCount = 0;
let currentWeekLevel = 0;
for (const dayActivity of dailyData) {
const date = new Date(dayActivity.date);
const dayOfWeek = date.getDay(); // 0 = Sunday, 1 = Monday, etc.
// If it's Sunday or we don't have a current week, start a new week
if (dayOfWeek === 0 || currentWeekStart === null) {
// Save the previous week if it exists
if (currentWeekStart !== null) {
weeklyData.push({
date: currentWeekStart.toISOString().split('T')[0],
count: currentWeekCount,
level: currentWeekLevel,
});
}
// Start new week
currentWeekStart = new Date(date);
currentWeekCount = dayActivity.count;
currentWeekLevel = dayActivity.level;
} else {
// Add to current week
currentWeekCount += dayActivity.count;
// Use the maximum level for the week
currentWeekLevel = Math.max(currentWeekLevel, dayActivity.level);
}
}
// Don't forget the last week
if (currentWeekStart !== null) {
weeklyData.push({
date: currentWeekStart.toISOString().split('T')[0],
count: currentWeekCount,
level: currentWeekLevel,
});
}
return weeklyData;
};
export const getWeekDateRange = (weekStartDate: string): string => {
const startDate = new Date(weekStartDate);
const endDate = new Date(startDate);
endDate.setDate(startDate.getDate() + 6);
const formatDate = (date: Date) => {
return date.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric'
});
};
return `${formatDate(startDate)} - ${formatDate(endDate)}`;
}; |