Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion lib/calculate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ describe('calculateMonthlyStats', () => {

expect(result.previousMonthTotal).toBe(0);
expect(result.currentMonthTotal).toBe(10);
expect(result.deltaPercentage).toBe(100);
expect(result.deltaPercentage).toBeNull();
});

it('handles zero current month contributions', () => {
Expand Down
20 changes: 10 additions & 10 deletions lib/calculate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,16 @@ export function calculateMonthlyStats(
}).format(now);

const deltaAbsolute = currentMonthTotal - previousMonthTotal;
let deltaPercentage = 0;

if (previousMonthTotal === 0) {
if (currentMonthTotal > 0) {
deltaPercentage = 100;
}
} else {
deltaPercentage = Math.round((deltaAbsolute / previousMonthTotal) * 100);
if (deltaPercentage === -0) deltaPercentage = 0;
}
// When there is no baseline (previous month = 0), the percentage change is
// mathematically undefined. Return null so the renderer can display 'N/A'
// instead of the misleading hardcoded +100%.
const deltaPercentage: number | null =
previousMonthTotal === 0
? null
: (() => {
const pct = Math.round((deltaAbsolute / previousMonthTotal) * 100);
return pct === -0 ? 0 : pct;
})();

return {
currentMonthTotal,
Expand Down
48 changes: 28 additions & 20 deletions lib/svg/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,18 +434,22 @@ export function generateMonthlySVG(stats: MonthlyStats, params: BadgeParams): st
: `${stats.deltaAbsolute} commits`;
} else if (params.delta_format === 'both') {
deltaText =
stats.deltaPercentage > 0
? `+${stats.deltaPercentage}% (+${stats.deltaAbsolute})`
: stats.deltaPercentage < 0
? `${stats.deltaPercentage}% (${stats.deltaAbsolute})`
: `0% (${stats.deltaAbsolute > 0 ? '+' : ''}${stats.deltaAbsolute})`;
stats.deltaPercentage === null
? `N/A (${stats.deltaAbsolute > 0 ? '+' : ''}${stats.deltaAbsolute})`
: stats.deltaPercentage > 0
? `+${stats.deltaPercentage}% (+${stats.deltaAbsolute})`
: stats.deltaPercentage < 0
? `${stats.deltaPercentage}% (${stats.deltaAbsolute})`
: `0% (${stats.deltaAbsolute > 0 ? '+' : ''}${stats.deltaAbsolute})`;
} else {
deltaText =
stats.deltaPercentage > 0
? `+${stats.deltaPercentage}%`
: stats.deltaPercentage < 0
? `${stats.deltaPercentage}%`
: `0%`;
stats.deltaPercentage === null
? 'N/A'
: stats.deltaPercentage > 0
? `+${stats.deltaPercentage}%`
: stats.deltaPercentage < 0
? `${stats.deltaPercentage}%`
: `0%`;
}
const deltaColor = stats.deltaAbsolute >= 0 ? accent : '#ff4444';

Expand Down Expand Up @@ -514,18 +518,22 @@ function generateAutoThemeMonthlySVG(stats: MonthlyStats, params: BadgeParams):
: `${stats.deltaAbsolute} commits`;
} else if (params.delta_format === 'both') {
deltaText =
stats.deltaPercentage > 0
? `+${stats.deltaPercentage}% (+${stats.deltaAbsolute})`
: stats.deltaPercentage < 0
? `${stats.deltaPercentage}% (${stats.deltaAbsolute})`
: `0% (${stats.deltaAbsolute > 0 ? '+' : ''}${stats.deltaAbsolute})`;
stats.deltaPercentage === null
? `N/A (${stats.deltaAbsolute > 0 ? '+' : ''}${stats.deltaAbsolute})`
: stats.deltaPercentage > 0
? `+${stats.deltaPercentage}% (+${stats.deltaAbsolute})`
: stats.deltaPercentage < 0
? `${stats.deltaPercentage}% (${stats.deltaAbsolute})`
: `0% (${stats.deltaAbsolute > 0 ? '+' : ''}${stats.deltaAbsolute})`;
} else {
deltaText =
stats.deltaPercentage > 0
? `+${stats.deltaPercentage}%`
: stats.deltaPercentage < 0
? `${stats.deltaPercentage}%`
: `0%`;
stats.deltaPercentage === null
? 'N/A'
: stats.deltaPercentage > 0
? `+${stats.deltaPercentage}%`
: stats.deltaPercentage < 0
? `${stats.deltaPercentage}%`
: `0%`;
}

return `
Expand Down
4 changes: 2 additions & 2 deletions types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export interface MonthlyStats {
/** Total number of contributions in the previous calendar month. */
previousMonthTotal: number;

/** Percentage change in contributions compared to the previous month (can be negative). */
deltaPercentage: number;
/** Percentage change in contributions compared to the previous month (can be negative). Null when previous month has zero contributions (undefined baseline). */
deltaPercentage: number | null;

/** Absolute change in contribution count compared to the previous month (can be negative). */
deltaAbsolute: number;
Expand Down
Loading