Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/lib/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,16 @@ function calculateStreaks(calendar: { count: number }[]): { longestStreak: numbe

function calculateMostActiveDay(calendar: { date: string; count: number }[]): string | null {
const weekdayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const weekdayTotals = Array.from({ length: 7 }, () => 0);

for (const day of calendar) {
if (day.count === 0) {
continue;
const weekdayTotals = calendar.reduce((totals, day) => {
if (day.count > 0) {
totals[new Date(`${day.date}T00:00:00Z`).getUTCDay()] += day.count;
}
const weekday = new Date(`${day.date}T00:00:00Z`).getUTCDay();
weekdayTotals[weekday] += day.count;
}
return totals;
}, Array(7).fill(0));
Comment on lines +93 to +98

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using Array(7).fill(0) can lead to looser type inference (e.g., any[] in some TypeScript configurations) and carries slight runtime overhead compared to a literal array. Using a literal array [0, 0, 0, 0, 0, 0, 0] is more idiomatic, guarantees strict number[] type inference, and is slightly more performant.

Suggested change
const weekdayTotals = calendar.reduce((totals, day) => {
if (day.count > 0) {
totals[new Date(`${day.date}T00:00:00Z`).getUTCDay()] += day.count;
}
const weekday = new Date(`${day.date}T00:00:00Z`).getUTCDay();
weekdayTotals[weekday] += day.count;
}
return totals;
}, Array(7).fill(0));
const weekdayTotals = calendar.reduce((totals: number[], day: any): number[] => {
if (day.count > 0) {
totals[new Date(day.date + "T00:00:00Z").getUTCDay()] += day.count;
}
return totals;
}, [0, 0, 0, 0, 0, 0, 0]);
References
  1. Maintain explicit return types for functions in TypeScript to ensure type safety and API clarity.


const maxWeekdayTotal = Math.max(...weekdayTotals);
return maxWeekdayTotal > 0
? weekdayNames[weekdayTotals.findIndex((count) => count === maxWeekdayTotal)]
? weekdayNames[weekdayTotals.indexOf(maxWeekdayTotal)]
: null;
}

Expand Down
Loading