Skip to content

Commit a8b7115

Browse files
committed
Modernize JS code
1 parent 537723a commit a8b7115

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

src/js.js

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44
const allowedQueryParams = new Set(['user', 'repo', 'type', 'count', 'size', 'text', 'v']);
55

66
function getUrlParameters() {
7-
// TODO: Replace with URLSearchParams later
8-
const hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
7+
const searchParams = new URLSearchParams(globalThis.location.search);
98
const parameters = new Map();
109

11-
for (const hash of hashes) {
12-
const [parameter, value] = hash.split('=');
13-
10+
for (const [parameter, value] of searchParams.entries()) {
1411
if (allowedQueryParams.has(parameter)) {
1512
parameters.set(parameter, value);
1613
}
@@ -19,17 +16,27 @@
1916
return parameters;
2017
}
2118

22-
// Add commas to numbers
19+
// Format numbers
2320
function addCommas(n) {
24-
// eslint-disable-next-line unicorn/prefer-string-replace-all
25-
return String(n).replace(/(\d)(?=(\d{3})+$)/g, '$1,');
21+
return new Intl.NumberFormat('en-US').format(n);
2622
}
2723

28-
function jsonp(path) {
29-
const script = document.createElement('script');
24+
async function fetchData(path) {
25+
try {
26+
const response = await fetch(path);
27+
if (!response.ok) {
28+
throw new Error('Network response was not ok');
29+
}
3030

31-
script.src = `${path}?callback=callback`;
32-
document.head.insertBefore(script, document.head.firstChild);
31+
return await response.json();
32+
} catch (error) {
33+
console.error(`Error fetching data: ${error}`);
34+
return {
35+
data: {
36+
message: 'Not Found'
37+
}
38+
};
39+
}
3340
}
3441

3542
// Parameters
@@ -55,38 +62,38 @@
5562
const REPO_URL = `${GITHUB_URL + user}/${repo}`;
5663
const USER_REPO = `${user}/${repo}`;
5764

58-
window.callback = function(obj) {
59-
if (obj.data.message === 'Not Found') {
65+
async function processApiData(data) {
66+
if (data.message === 'Not Found') {
6067
return;
6168
}
6269

6370
switch (type) {
6471
case 'watch': {
6572
if (v === '2') {
66-
counter.textContent = obj.data.subscribers_count && addCommas(obj.data.subscribers_count);
73+
counter.textContent = data.subscribers_count && addCommas(data.subscribers_count);
6774
counter.setAttribute('aria-label', `${counter.textContent} watchers ${LABEL_SUFFIX}`);
6875
} else {
69-
counter.textContent = obj.data.stargazers_count && addCommas(obj.data.stargazers_count);
76+
counter.textContent = data.stargazers_count && addCommas(data.stargazers_count);
7077
counter.setAttribute('aria-label', `${counter.textContent} stargazers ${LABEL_SUFFIX}`);
7178
}
7279

7380
break;
7481
}
7582

7683
case 'star': {
77-
counter.textContent = obj.data.stargazers_count && addCommas(obj.data.stargazers_count);
84+
counter.textContent = data.stargazers_count && addCommas(data.stargazers_count);
7885
counter.setAttribute('aria-label', `${counter.textContent} stargazers ${LABEL_SUFFIX}`);
7986
break;
8087
}
8188

8289
case 'fork': {
83-
counter.textContent = obj.data.network_count && addCommas(obj.data.network_count);
90+
counter.textContent = data.network_count && addCommas(data.network_count);
8491
counter.setAttribute('aria-label', `${counter.textContent} forks ${LABEL_SUFFIX}`);
8592
break;
8693
}
8794

8895
case 'follow': {
89-
counter.textContent = obj.data.followers && addCommas(obj.data.followers);
96+
counter.textContent = data.followers && addCommas(data.followers);
9097
counter.setAttribute('aria-label', `${counter.textContent} followers ${LABEL_SUFFIX}`);
9198
break;
9299
}
@@ -97,7 +104,7 @@
97104
counter.style.display = 'block';
98105
counter.removeAttribute('aria-hidden');
99106
}
100-
};
107+
}
101108

102109
// Set href to be URL for repo
103110
button.href = REPO_URL;
@@ -177,9 +184,14 @@
177184
return;
178185
}
179186

180-
if (type === 'follow') {
181-
jsonp(`${API_URL}users/${user}`);
182-
} else {
183-
jsonp(`${API_URL}repos/${user}/${repo}`);
184-
}
187+
(async() => {
188+
try {
189+
const apiPath = type === 'follow' ? `${API_URL}users/${user}` : `${API_URL}repos/${user}/${repo}`;
190+
const data = await fetchData(apiPath);
191+
192+
await processApiData(data);
193+
} catch (error) {
194+
console.error(`Error fetching GitHub data: ${error}`);
195+
}
196+
})();
185197
})();

xo.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ module.exports = [
3737
'default-case': 'off',
3838
'prefer-template': 'error',
3939
'unicorn/no-negated-condition': 'off',
40-
'unicorn/prefer-global-this': 'off',
4140
'unicorn/prefer-module': 'off',
42-
'unicorn/prefer-top-level-await': 'off',
4341
'unicorn/prevent-abbreviations': 'off'
4442
}
4543
}

0 commit comments

Comments
 (0)