forked from nilbuild/developer-roadmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.ts
More file actions
95 lines (81 loc) · 2.25 KB
/
github.ts
File metadata and controls
95 lines (81 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const formatter = Intl.NumberFormat('en-US', {
notation: 'compact',
});
const defaultStarCount = 224000;
let starCount: number | undefined = undefined;
export async function countStars(
repo = 'kamranahmedse/developer-roadmap',
): Promise<number> {
if (starCount) {
return starCount;
}
try {
const repoData = await fetch(`https://api.github.com/repos/${repo}`);
const json = await repoData.json();
starCount = json.stargazers_count * 1 || defaultStarCount;
} catch (e) {
console.log('Failed to fetch stars', e);
starCount = defaultStarCount;
}
return starCount;
}
export async function getFormattedStars(
repo = 'kamranahmedse/developer-roadmap',
): Promise<string> {
const stars = import.meta.env.DEV ? defaultStarCount : await countStars(repo);
return formatter.format(stars);
}
const defaultRanking = '7th';
let ranking: string;
export async function getRepositoryRank(
repo = 'kamranahmedse/developer-roadmap',
): Promise<string> {
try {
const response = await fetch(
`https://api.github.com/search/repositories?q=stars:>100000&o=desc&s=stars`,
);
const json = await response.json();
const repositories = json.items || [];
for (let rank = 1; rank <= repositories.length; rank++) {
if (
repositories[rank - 1].full_name.toLowerCase() === repo.toLowerCase()
) {
ranking = `${rank}${getOrdinalSuffix(rank)}`;
}
}
} catch (e) {
console.log('Failed to fetch ranking');
ranking = defaultRanking;
}
return ranking;
}
function getOrdinalSuffix(rank: number): string {
if (11 <= rank % 100 && rank % 100 <= 13) {
return 'th';
}
switch (rank % 10) {
case 1:
return 'st';
case 2:
return 'nd';
case 3:
return 'rd';
default:
return 'th';
}
}
// fetches the top languages of a GitHub repository
// i.e. 90% of the bytes are in these languages
export function getTopGitHubLanguages(languages: Record<string, number>) {
const total = Object.values(languages).reduce((a, b) => a + b, 0);
let sum = 0;
let topLanguages = [];
for (const [language, bytes] of Object.entries(languages)) {
sum += bytes;
topLanguages.push(language);
if (sum / total >= 0.9) {
break;
}
}
return topLanguages;
}