UNPKG

contribution

Version:

GitHub contribution graph parser - contribution streak & statistic calculator with zero dependencies

60 lines (53 loc) 2.59 kB
function $parcel$export(e, n, v, s) { Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true}); } $parcel$export(module.exports, "fetchGitHubStats", () => $8041223c18bdc723$export$43702e26844fe66a); const $52fb07161c3553f5$export$4d7b59bac733eef2 = (body)=>{ const bodyMatches = body.matchAll(/data-date="([\d-]+)".*?data-level="(\d+)".*\n.*>(\d+|No) contribution/g); const contributions = {}; for (const [_, date, level, contribution] of bodyMatches)contributions[date] = { contributions: contribution === 'No' ? 0 : Number.parseInt(contribution), gitHubLegendLevel: Number.parseInt(level) }; const sortedContributions = Object.fromEntries(Object.entries(contributions).sort(([a], [b])=>+new Date(a) - +new Date(b))); return sortedContributions; }; const $52fb07161c3553f5$export$ded3b9318d32c3e2 = (contributions)=>{ const stats = { bestStreak: 0, currentStreak: 0, previousStreak: 0, isStreakAtRisk: false, mostContributions: 0, todaysContributions: 0, totalContributions: 0, contributions: contributions }; let previousStreak = 0; for (const [_, day] of Object.entries(contributions)){ // Contributions stats.totalContributions += day.contributions; stats.todaysContributions = day.contributions; if (day.contributions > stats.mostContributions) stats.mostContributions = day.contributions; // Streak stats.currentStreak = day.contributions > 0 ? stats.currentStreak + 1 : 0; if (stats.currentStreak > stats.bestStreak) stats.bestStreak = stats.currentStreak; stats.isStreakAtRisk = stats.currentStreak === 0 && previousStreak > 0; if (stats.isStreakAtRisk) stats.previousStreak = previousStreak; previousStreak = stats.currentStreak; } return stats; }; const $8041223c18bdc723$export$43702e26844fe66a = async (username)=>{ try { const contributionGraphResponse = await fetch(`https://github.com/users/${username}/contributions`); if (!contributionGraphResponse.ok) throw new Error('Failed to fetch GitHub contributions'); const contributionGraphBody = await contributionGraphResponse.text(); const contributions = (0, $52fb07161c3553f5$export$4d7b59bac733eef2)(contributionGraphBody); const gitHubStats = (0, $52fb07161c3553f5$export$ded3b9318d32c3e2)(contributions); return gitHubStats; } catch (error) { throw new Error(error); } }; //# sourceMappingURL=contribution.js.map