Skip to content

Commit 34958a1

Browse files
authored
docs(calculate): add JSDoc for streak and montly stats calculation (JhaSourav07#472)
## Description Fixes JhaSourav07#303 Added comprehensive JSDoc documentation in `lib/calculate.ts`. Changes included: - Added complete `@param` documentation for function parameters - Documented parameter types and default values - Added explanation for timezone handling and `now` override behavior - Documented the one-day grace-period logic used in streak calculations - Added usage examples showing timezone override and custom date injection - Added documentation for monthly statistics calculation No functional logic was changed. This PR only improves maintainability and onboarding for new contributors. ## Pillar * [ ] 🎨 Pillar 1 — New Theme Design * [ ] 📐 Pillar 2 — Geometric SVG Improvement * [ ] 🕐 Pillar 3 — Timezone Logic Optimization * [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview N/A — Documentation-only change ## Checklist before requesting a review: * [x] I have read the `CONTRIBUTING.md` file. * [x] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). * [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). * [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). * [ ] I have updated `README.md` if I added a new theme or URL parameter. * [x] I have started the repo. * [x] I have made sure that I have only one commit to merge in this PR. * [x] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). * [ ] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents 8af10bd + 7e9a0ca commit 34958a1

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

lib/calculate.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,56 @@
11
// lib/calculate.ts
22
import type { ContributionCalendar, StreakStats, MonthlyStats } from '../types';
33

4+
/*
5+
Calculates streak statistics from a GitHub contribution calendar.
6+
7+
This function computes:
8+
- Current streak length (with a one-day grace period)
9+
- Longest streak length across the calendar
10+
- Total contributions made
11+
- Effective "today" date used for UI display
12+
13+
Current Streak Logic:
14+
The streak remains active if there are no contributions today
15+
but there were contributions yesterday. This prevents streaks
16+
from breaking prematurely due to timezone differences.
17+
18+
Timezone Handling:
19+
Contribution dates are calendar-based. The provided IANA timezone
20+
determines what "today" means for the user, ensuring streaks are
21+
calculated correctly in local time (e.g., "Asia/Kolkata",
22+
"America/Los_Angeles"). Defaults to "UTC".
23+
24+
@param {ContributionCalendar} calendar
25+
GitHub contribution calendar data containing weekly entries.
26+
27+
@param {string} [timezone='UTC']
28+
IANA timezone string used to determine the local date.
29+
30+
@param {Date} [now=new Date()]
31+
Current time reference for calculations. Useful for testing
32+
or overriding the system clock.
33+
34+
@returns {StreakStats}
35+
An object with:
36+
- currentStreak: active streak length
37+
- longestStreak: longest historical streak
38+
- totalContributions: total contribution count
39+
- todayDate: effective date used for streak/UI calculations
40+
41+
@example
42+
// Calculate streak stats using a specific timezone
43+
const stats = calculateStreak(calendar, 'Asia/Kolkata');
44+
45+
@example
46+
// Override both timezone and current time (useful in tests)
47+
const stats = calculateStreak(
48+
calendar,
49+
'America/Los_Angeles',
50+
new Date('2026-05-25T10:00:00Z')
51+
);
52+
*/
53+
454
export function calculateStreak(
555
calendar: ContributionCalendar,
656
timezone: string = 'UTC',
@@ -77,6 +127,34 @@ export function calculateStreak(
77127
};
78128
}
79129

130+
/*
131+
Calculates monthly contribution statistics from a GitHub contribution calendar.
132+
133+
Figures out how many contributions were made this month and last month, then compares them.
134+
135+
@param {ContributionCalendar} calendar
136+
Weekly GitHub contribution data.
137+
138+
@param {string} [timezone='UTC']
139+
Timezone to decide which month is "current".
140+
Example: 'Asia/Kolkata' ensures local month is used.
141+
142+
@param {Date} [now=new Date()]
143+
Current date/time reference (useful for testing).
144+
145+
@returns {MonthlyStats}
146+
- currentMonthTotal: contributions this month
147+
- previousMonthTotal: contributions last month
148+
- deltaAbsolute: difference (this − last)
149+
- deltaPercentage: % change, rounded
150+
- currentMonthName: name of this month (e.g. "May")
151+
152+
@example
153+
const stats = calculateMonthlyStats(calendar, 'Asia/Kolkata');
154+
stats.currentMonthName → "May"
155+
stats.deltaPercentage → 42
156+
*/
157+
80158
export function calculateMonthlyStats(
81159
calendar: ContributionCalendar,
82160
timezone: string = 'UTC',

0 commit comments

Comments
 (0)