Skip to content

Commit 166ab57

Browse files
committed
refactor(calculate): extract isStreakAlive helper
1 parent e15a46a commit 166ab57

2 files changed

Lines changed: 31 additions & 3 deletions

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,
@@ -259,6 +259,28 @@ describe('calculateStreak — timezone awareness', () => {
259259
});
260260
});
261261

262+
describe('isStreakAlive', () => {
263+
it('returns true when both today and yesterday have contributions', () => {
264+
expect(isStreakAlive({ contributionCount: 1 }, { contributionCount: 1 })).toBe(true);
265+
});
266+
267+
it('returns true when only today has contributions', () => {
268+
expect(isStreakAlive({ contributionCount: 1 }, { contributionCount: 0 })).toBe(true);
269+
});
270+
271+
it('returns true when only yesterday has contributions', () => {
272+
expect(isStreakAlive({ contributionCount: 0 }, { contributionCount: 1 })).toBe(true);
273+
});
274+
275+
it('returns false when both today and yesterday have zero contributions', () => {
276+
expect(isStreakAlive({ contributionCount: 0 }, { contributionCount: 0 })).toBe(false);
277+
});
278+
279+
it('returns false when yesterday is null and today has no contributions', () => {
280+
expect(isStreakAlive({ contributionCount: 0 }, null)).toBe(false);
281+
});
282+
});
283+
262284
describe('calculateMonthlyStats', () => {
263285
it('calculates monthly stats correctly when both months have commits', () => {
264286
const calendar = {

lib/calculate.ts

Lines changed: 8 additions & 2 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,
@@ -98,9 +104,9 @@ export function calculateStreak(
98104
// If I committed today, the streak is alive.
99105
// If I haven't committed today, but I committed yesterday,
100106
// the streak is STILL alive (Grace Period).
101-
const isStreakAlive = today.contributionCount > 0 || (yesterday?.contributionCount ?? 0) > 0;
107+
const streakAlive = isStreakAlive(today, yesterday);
102108

103-
if (isStreakAlive) {
109+
if (streakAlive) {
104110
// Count backwards from the first day that has a contribution
105111
// starting from either today or yesterday.
106112
let i = today.contributionCount > 0 ? todayIndex : todayIndex - 1;

0 commit comments

Comments
 (0)