Skip to content

Commit e8de0b1

Browse files
authored
refactor(github): extract buildCommitClock helper (JhaSourav07#752)
## Description Fixes JhaSourav07#299 Extracted commit clock aggregation logic into `buildCommitClock()` helper. Added tests for: - per-day contribution aggregation - helper output correctness Existing `getFullDashboardData` commitClock tests continue to pass. ## Pillar - [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview N/A — backend refactor only. ## Checklist before requesting a review: - [x] I have read the CONTRIBUTING.md file. - [x] I have tested these changes locally. - [x] I have run npm run format and npm run lint locally. - [x] My commits follow Conventional Commits format. - [x] I have made sure I have only one commit in this PR.
2 parents 3bc4bf6 + 0076c60 commit e8de0b1

3 files changed

Lines changed: 53 additions & 41 deletions

File tree

lib/github.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
GITHUB_CACHE_TTL_MS,
1111
validateGitHubUsername,
1212
cacheKey,
13+
buildCommitClock,
1314
} from './github';
1415
import type { ContributionCalendar } from '../types';
1516

@@ -562,3 +563,26 @@ describe('cacheKey', () => {
562563
expect(cacheKey('contributions', 'testuser')).toContain('contributions');
563564
});
564565
});
566+
567+
describe('buildCommitClock', () => {
568+
it('aggregates commits correctly by day of week', () => {
569+
const allDays = [
570+
{ date: '2024-06-09', contributionCount: 2 }, // Sun
571+
{ date: '2024-06-10', contributionCount: 5 }, // Mon
572+
{ date: '2024-06-10', contributionCount: 3 }, // Mon
573+
{ date: '2024-06-12', contributionCount: 4 }, // Wed
574+
];
575+
576+
const result = buildCommitClock(allDays);
577+
578+
expect(result).toEqual([
579+
{ day: 'Sun', commits: 2 },
580+
{ day: 'Mon', commits: 8 },
581+
{ day: 'Tue', commits: 0 },
582+
{ day: 'Wed', commits: 4 },
583+
{ day: 'Thu', commits: 0 },
584+
{ day: 'Fri', commits: 0 },
585+
{ day: 'Sat', commits: 0 },
586+
]);
587+
});
588+
});

lib/github.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// lib/github.ts
22

3-
import type { ContributionCalendar } from '../types';
3+
import type { ContributionCalendar, ContributionDay } from '../types';
44
import { calculateStreak } from './calculate';
55
import { TTLCache } from './cache';
66
import { LANGUAGE_COLORS } from './svg/languageColors';
@@ -365,6 +365,21 @@ export function generateAchievements(totalContributions: number, currentStreak:
365365
return achievements;
366366
}
367367

368+
export function buildCommitClock(allDays: ContributionDay[]) {
369+
const dayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
370+
const dayTotals = new Array(7).fill(0);
371+
372+
for (const day of allDays) {
373+
const dow = new Date(day.date).getUTCDay();
374+
dayTotals[dow] += day.contributionCount;
375+
}
376+
377+
return dayNames.map((name, i) => ({
378+
day: name,
379+
commits: dayTotals[i],
380+
}));
381+
}
382+
368383
export async function getFullDashboardData(username: string, options: FetchOptions = {}) {
369384
if (!validateGitHubUsername(username)) {
370385
console.warn(
@@ -518,18 +533,7 @@ export async function getFullDashboardData(username: string, options: FetchOptio
518533
text: `Your longest coding streak is ${streakStats.longestStreak} days!`,
519534
});
520535
}
521-
522-
// Aggregate real contribution data by day of week from the already-fetched calendar
523-
const dayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
524-
const dayTotals = new Array(7).fill(0);
525-
for (const day of allDays) {
526-
const dow = new Date(day.date).getUTCDay();
527-
dayTotals[dow] += day.contributionCount;
528-
}
529-
const commitClock = dayNames.map((name, i) => ({
530-
day: name,
531-
commits: dayTotals[i],
532-
}));
536+
const commitClock = buildCommitClock(allDays);
533537

534538
return {
535539
profile,

package-lock.json

Lines changed: 12 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)