Skip to content

Commit b7e88ba

Browse files
committed
Modernize JS code
1 parent b9fd6f1 commit b7e88ba

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

src/js.js

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
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-
14-
if (allowedQueryParams.has(parameter)) {
15-
parameters.set(parameter, value);
10+
for (const [param, value] of searchParams.entries()) {
11+
if (allowedQueryParams.has(param)) {
12+
parameters.set(param, value);
1613
}
1714
}
1815

@@ -21,15 +18,21 @@
2118

2219
// Add commas to 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().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 { data: { message: 'Not Found' } };
35+
}
3336
}
3437

3538
// Parameters
@@ -55,38 +58,38 @@
5558
const REPO_URL = `${GITHUB_URL + user}/${repo}`;
5659
const USER_REPO = `${user}/${repo}`;
5760

58-
window.callback = function(obj) {
59-
if (obj.data.message === 'Not Found') {
61+
async function processApiData(data) {
62+
if (data.message === 'Not Found') {
6063
return;
6164
}
6265

6366
switch (type) {
6467
case 'watch': {
6568
if (v === '2') {
66-
counter.textContent = obj.data.subscribers_count && addCommas(obj.data.subscribers_count);
69+
counter.textContent = data.subscribers_count && addCommas(data.subscribers_count);
6770
counter.setAttribute('aria-label', `${counter.textContent} watchers ${LABEL_SUFFIX}`);
6871
} else {
69-
counter.textContent = obj.data.stargazers_count && addCommas(obj.data.stargazers_count);
72+
counter.textContent = data.stargazers_count && addCommas(data.stargazers_count);
7073
counter.setAttribute('aria-label', `${counter.textContent} stargazers ${LABEL_SUFFIX}`);
7174
}
7275

7376
break;
7477
}
7578

7679
case 'star': {
77-
counter.textContent = obj.data.stargazers_count && addCommas(obj.data.stargazers_count);
80+
counter.textContent = data.stargazers_count && addCommas(data.stargazers_count);
7881
counter.setAttribute('aria-label', `${counter.textContent} stargazers ${LABEL_SUFFIX}`);
7982
break;
8083
}
8184

8285
case 'fork': {
83-
counter.textContent = obj.data.network_count && addCommas(obj.data.network_count);
86+
counter.textContent = data.network_count && addCommas(data.network_count);
8487
counter.setAttribute('aria-label', `${counter.textContent} forks ${LABEL_SUFFIX}`);
8588
break;
8689
}
8790

8891
case 'follow': {
89-
counter.textContent = obj.data.followers && addCommas(obj.data.followers);
92+
counter.textContent = data.followers && addCommas(data.followers);
9093
counter.setAttribute('aria-label', `${counter.textContent} followers ${LABEL_SUFFIX}`);
9194
break;
9295
}
@@ -97,7 +100,7 @@
97100
counter.style.display = 'block';
98101
counter.removeAttribute('aria-hidden');
99102
}
100-
};
103+
}
101104

102105
// Set href to be URL for repo
103106
button.href = REPO_URL;
@@ -177,9 +180,14 @@
177180
return;
178181
}
179182

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

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)