Skip to content

Commit 1dea200

Browse files
authored
refactor(calculate): extract isStreakAlive helper (JhaSourav07#761)
## Description Fixes JhaSourav07#304 Extracted the grace-period streak logic into a dedicated `isStreakAlive()` helper to improve readability and testability in `calculateStreak`. ## Changes made - Added and exported `isStreakAlive()` helper - Replaced inline grace-period boolean logic in `calculateStreak` - Added dedicated helper tests covering: - both active - only today active - only yesterday active - both inactive - null yesterday edge case ## 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 (logic refactor and test changes) ## 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 and resolved all errors. - [x] My commits follow the Conventional Commits format. - [ ] 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. - [x] I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents 85e0959 + 5e94c54 commit 1dea200

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

lib/calculate.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from 'vitest';
2-
import { calculateStreak, calculateMonthlyStats } from './calculate';
2+
import { calculateStreak, calculateMonthlyStats, isStreakAlive } from './calculate';
33
import type { ContributionCalendar } from '../types';
44

55
// Turns a flat array of daily counts into the ContributionCalendar shape,
@@ -289,6 +289,28 @@ describe('calculateStreak — timezone awareness', () => {
289289
});
290290
});
291291

292+
describe('isStreakAlive', () => {
293+
it('returns true when both today and yesterday have contributions', () => {
294+
expect(isStreakAlive({ contributionCount: 1 }, { contributionCount: 1 })).toBe(true);
295+
});
296+
297+
it('returns true when only today has contributions', () => {
298+
expect(isStreakAlive({ contributionCount: 1 }, { contributionCount: 0 })).toBe(true);
299+
});
300+
301+
it('returns true when only yesterday has contributions', () => {
302+
expect(isStreakAlive({ contributionCount: 0 }, { contributionCount: 1 })).toBe(true);
303+
});
304+
305+
it('returns false when both today and yesterday have zero contributions', () => {
306+
expect(isStreakAlive({ contributionCount: 0 }, { contributionCount: 0 })).toBe(false);
307+
});
308+
309+
it('returns false when yesterday is null and today has no contributions', () => {
310+
expect(isStreakAlive({ contributionCount: 0 }, null)).toBe(false);
311+
});
312+
});
313+
292314
describe('calculateMonthlyStats', () => {
293315
it('calculates monthly stats correctly when both months have commits', () => {
294316
const calendar = {

lib/calculate.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ const stats = calculateStreak(
5050
new Date('2026-05-25T10:00:00Z')
5151
);
5252
*/
53+
export function isStreakAlive(
54+
today: { contributionCount: number },
55+
yesterday: { contributionCount: number } | null
56+
): boolean {
57+
return today.contributionCount > 0 || (yesterday?.contributionCount ?? 0) > 0;
58+
}
5359

5460
export function calculateStreak(
5561
calendar: ContributionCalendar,

0 commit comments

Comments
 (0)