Repository navigation
Expand file tree
/
Copy pathgithubMapper.ts
More file actions
132 lines (115 loc) · 3.54 KB
/
Copy pathgithubMapper.ts
File metadata and controls
132 lines (115 loc) · 3.54 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { SOCIALS } from '../data/socials';
import { TECHNOLOGIES } from '../data/technologies';
import { sanitizeSocialUrl } from './urlSanitizer';
export interface GitHubUser {
name: string | null;
bio: string | null;
blog: string | null;
twitter_username: string | null;
email: string | null;
}
export interface GitHubRepo {
language: string | null;
}
export interface GitHubSocialAccount {
provider: string;
url: string;
}
export interface ImportedData {
name: string;
description: string;
selectedTechs: string[];
selectedSocials: string[];
socialLinks: Record<string, string>;
}
const languageAliasMap: Record<string, string> = {
'c++': 'cplusplus',
'c#': 'csharp',
html: 'html5',
css: 'css3',
vue: 'vuejs',
scss: 'sass',
shell: 'bash',
'jupyter notebook': 'python',
'objective-c': 'apple',
swift: 'swift',
};
export function mapGitHubData(
user: GitHubUser,
repos: GitHubRepo[],
socialAccounts: GitHubSocialAccount[]
): ImportedData {
const result: ImportedData = {
name: user.name || '',
description: user.bio || '',
selectedTechs: [],
selectedSocials: [],
socialLinks: {},
};
if (user.twitter_username) {
const twitterSocial = SOCIALS.find((s) => s.id === 'twitter');
if (twitterSocial && !result.selectedSocials.includes(twitterSocial.id)) {
result.selectedSocials.push(twitterSocial.id);
result.socialLinks[twitterSocial.id] = user.twitter_username;
}
}
if (user.blog) {
let url = user.blog;
if (!url.startsWith('http://') && !url.startsWith('https://')) {
url = `https://${url}`;
}
const websiteSocial = SOCIALS.find((s) => s.id === 'website');
if (websiteSocial && !result.selectedSocials.includes(websiteSocial.id)) {
result.selectedSocials.push(websiteSocial.id);
result.socialLinks[websiteSocial.id] = url;
}
}
if (user.email) {
const emailSocial = SOCIALS.find((s) => s.id === 'email');
if (emailSocial && !result.selectedSocials.includes(emailSocial.id)) {
result.selectedSocials.push(emailSocial.id);
result.socialLinks[emailSocial.id] = `mailto:${user.email}`;
}
}
socialAccounts.forEach((account) => {
const providerStr = account.provider.toLowerCase();
const matchingSocial = SOCIALS.find(
(s) =>
s.name.toLowerCase() === providerStr ||
s.id.toLowerCase() === providerStr ||
account.url.toLowerCase().includes(s.id.toLowerCase())
);
if (matchingSocial && !result.selectedSocials.includes(matchingSocial.id)) {
result.selectedSocials.push(matchingSocial.id);
result.socialLinks[matchingSocial.id] = sanitizeSocialUrl(matchingSocial.id, account.url);
}
});
const languageCounts: Record<string, number> = {};
repos.forEach((repo) => {
if (repo.language) {
const langStr = repo.language.toLowerCase();
languageCounts[langStr] = (languageCounts[langStr] || 0) + 1;
}
});
const sortedLanguages = Object.entries(languageCounts)
.sort((a, b) => b[1] - a[1])
.map(([lang]) => lang);
sortedLanguages.forEach((lang) => {
let techId: string | null = null;
if (languageAliasMap[lang]) {
techId = languageAliasMap[lang];
} else {
const matchingTech = TECHNOLOGIES.find(
(t) =>
t.id === lang || t.name.toLowerCase() === lang || t.id === lang.replace(/[^a-z0-9]/g, '')
);
if (matchingTech) {
techId = matchingTech.id;
}
}
if (techId && !result.selectedTechs.includes(techId)) {
result.selectedTechs.push(techId);
}
});
return result;
}