From 32e3b5b9a3bc59d055d255050e900c03cb09e9fb Mon Sep 17 00:00:00 2001 From: lifeadventurer Date: Sat, 12 Oct 2024 00:27:15 +0800 Subject: [PATCH] Refactor: Use const instead of let for immutable variables (Make LSP happy) --- fortune_generator/js/fortune.js | 48 +++++++++++++------------- fortune_generator/js/matrix.js | 2 +- fortune_generator/js/scripts.js | 2 +- fortune_generator/js/service-worker.js | 10 +++--- quote_generator/js/scripts.js | 4 +-- scripts.js | 8 ++--- 6 files changed, 37 insertions(+), 37 deletions(-) diff --git a/fortune_generator/js/fortune.js b/fortune_generator/js/fortune.js index 7432e72..9a5a3ef 100644 --- a/fortune_generator/js/fortune.js +++ b/fortune_generator/js/fortune.js @@ -8,7 +8,7 @@ fetch("https://api.ipify.org?format=json").then(response => { }).then(res => { ip = res.ip; -}).catch(error => { +}).catch(_error => { if ('caches' in window) { caches.match('https://api.ipify.org?format=json').then(response => { if (response) { @@ -101,8 +101,8 @@ async function init_page() { $('#date').html(showDate); $('#weekday').html(showDay); - let showSpecialEventCount = 2, eventIndexPtr = 0; - let eventIndexList = Array(showSpecialEventCount).fill(-1); + const showSpecialEventCount = 2, eventIndexPtr = 0; + const eventIndexList = Array(showSpecialEventCount).fill(-1); // check if there is special event today for (let i = 0; i < special_events.length; i++) { if (daysDiff(i) > 0) { @@ -118,22 +118,22 @@ async function init_page() { // if there is upcoming event then show for (let eventIndex = 0; eventIndex < showSpecialEventCount; eventIndex++) { if (eventIndexList[eventIndex] != -1) { - let days = daysDiff(eventIndexList[eventIndex]); - let upcoming_event = `距離${special_events[eventIndexList[eventIndex]].event}還剩${days}`; + const days = daysDiff(eventIndexList[eventIndex]); + const upcoming_event = `距離${special_events[eventIndexList[eventIndex]].event}還剩${days}`; $(`#upcoming-event-${eventIndex + 1}`).html(upcoming_event) } } // show special event if today is a special day if (special) { - let special_event_today = `今日是${special_events[special_events_index].event}`; + const special_event_today = `今日是${special_events[special_events_index].event}`; $('#special-day').html(special_event_today); } - let last_date_str = localStorage.getItem('last_date'); + const last_date_str = localStorage.getItem('last_date'); if (last_date_str !== null && last_date_str !== undefined) { - let now_date = new Date(); - let last_date = new Date(last_date_str); + const now_date = new Date(); + const last_date = new Date(last_date_str); if (now_date.getFullYear() === last_date.getFullYear() && now_date.getMonth() === last_date.getMonth() && now_date.getDate() === last_date.getDate()) { fortune_generated = true; @@ -167,10 +167,10 @@ function Appear() { if (!fortune_generated) { // transform ip to four numbers - let num = ip.split(".").map(num => parseInt(num)); + const num = ip.split(".").map(num => parseInt(num)); // TODO: improve the hash process - let hashDate = Math.round(Math.log10(year * ((month << (Math.log10(num[3]) + day - 1)) * (date << Math.log10(num[2] << day))))); + const hashDate = Math.round(Math.log10(year * ((month << (Math.log10(num[3]) + day - 1)) * (date << Math.log10(num[2] << day))))); seed1 = (num[0] >> hashDate) * (num[1] >> Math.min(hashDate, 2)) + (num[2] << 1) * (num[3] >> 3) + (date << 3) * (month << hashDate) + (year * day) >> 2; seed2 = (num[0] << (hashDate + 2)) * (num[1] << hashDate) + (num[2] << 1) * (num[3] << 2) + (date << (hashDate - 1)) * (month << 4) + year >> hashDate + (date * day) >> 1; @@ -188,11 +188,11 @@ function Appear() { seed2 = parseInt(localStorage.getItem('last_seed2')); } - let status = `§ ${fortuneStatus[status_index]} §`; + const status = `§ ${fortuneStatus[status_index]} §`; if (special) { status_index = special_events[special_events_index].status_index; - let special_status = `§ ${fortuneStatus[status_index]} §`; + const special_status = `§ ${fortuneStatus[status_index]} §`; J_ip_to_fortune.html(special_status); } else { @@ -200,8 +200,8 @@ function Appear() { } // make sure the events won't collide - let set = new Set(); - let l1 = (seed1 % goodLen + goodLen) % goodLen; + const set = new Set(); + const l1 = (seed1 % goodLen + goodLen) % goodLen; set.add(goodFortunes[l1].event); let l2 = (((seed1 << 1) + date) % goodLen + goodLen) % goodLen; while (set.has(goodFortunes[l2].event)) { @@ -223,18 +223,18 @@ function Appear() { const l2_desc_list = goodFortunes[l2].description; const r1_desc_list = badFortunes[r1].description; const r2_desc_list = badFortunes[r2].description; - let l_1_event = good_span(goodFortunes[l1].event); - let l_1_desc = desc_span(l1_desc_list[Math.abs(seed1) % l1_desc_list.length]); - let l_2_event = good_span(goodFortunes[l2].event); - let l_2_desc = desc_span(l2_desc_list[Math.abs(seed2) % l2_desc_list.length]); - let r_1_event = bad_span(badFortunes[r1].event); - let r_1_desc = desc_span(r1_desc_list[Math.abs(seed1) % r1_desc_list.length]); - let r_2_event = bad_span(badFortunes[r2].event); - let r_2_desc = desc_span(r2_desc_list[Math.abs(seed2) % r2_desc_list.length]); + const l_1_event = good_span(goodFortunes[l1].event); + const l_1_desc = desc_span(l1_desc_list[Math.abs(seed1) % l1_desc_list.length]); + const l_2_event = good_span(goodFortunes[l2].event); + const l_2_desc = desc_span(l2_desc_list[Math.abs(seed2) % l2_desc_list.length]); + const r_1_event = bad_span(badFortunes[r1].event); + const r_1_desc = desc_span(r1_desc_list[Math.abs(seed1) % r1_desc_list.length]); + const r_2_event = bad_span(badFortunes[r2].event); + const r_2_desc = desc_span(r2_desc_list[Math.abs(seed2) % r2_desc_list.length]); if (special) { // instead clear variable name, use short variable name for here... cuz it's too repetitive - let Data = special_events[special_events_index]; + const Data = special_events[special_events_index]; if (status_index == 0) { J_r_1_event.html(allGood); } else { diff --git a/fortune_generator/js/matrix.js b/fortune_generator/js/matrix.js index ac1260d..d0acfd0 100644 --- a/fortune_generator/js/matrix.js +++ b/fortune_generator/js/matrix.js @@ -25,7 +25,7 @@ function Update() { context.fillRect(0, 0, canvas.width, canvas.height); if (frame == 0) { - let a = parseInt(Math.random() * 255); + const a = parseInt(Math.random() * 255); str = `rgba(${a}, ${Math.abs(a - 127)}, ${Math.abs(a - 255)}, 0.9)`; } context.fillStyle = str; diff --git a/fortune_generator/js/scripts.js b/fortune_generator/js/scripts.js index 6672e64..e4d7288 100644 --- a/fortune_generator/js/scripts.js +++ b/fortune_generator/js/scripts.js @@ -7,7 +7,7 @@ darkModeIcon.onclick = () => { function copyResultImageToClipboard() { try { - let $title = $('#title').clone().wrap('
'); + const $title = $('#title').clone().wrap('
'); $('#result-page').prepend($title.parent()); const backgroundColor = getComputedStyle($('.container')[0]).backgroundColor; diff --git a/fortune_generator/js/service-worker.js b/fortune_generator/js/service-worker.js index f650a20..b95591d 100644 --- a/fortune_generator/js/service-worker.js +++ b/fortune_generator/js/service-worker.js @@ -1,5 +1,5 @@ -let pre_cache_file_version = 'pre-v1.1.0'; -let auto_cache_file_version = 'auto-v1.1.0' +const pre_cache_file_version = 'pre-v1.1.0'; +const auto_cache_file_version = 'auto-v1.1.0' const ASSETS = [ '/generators/images/lifeadventurer-192x192.png', @@ -24,7 +24,7 @@ const NEED_UPDATE = [ 'https://api.ipify.org/?format=json', ] -let limit_cache_size = (name, size) => { +const limit_cache_size = (name, size) => { caches.open(name).then(cache => { cache.keys().then(keys => { if (keys.length > size) { @@ -36,7 +36,7 @@ let limit_cache_size = (name, size) => { }); }; -let is_in_array = (str, array) => { +const is_in_array = (str, array) => { let path = ''; // Check the request's domain is the same as the current domain. @@ -93,7 +93,7 @@ self.addEventListener('fetch', event => { throw new Error("Network response was not ok."); - }).catch(async error => { + }).catch(async _error => { const cache = await caches.open(auto_cache_file_version); return cache.match(event.request.url); }) diff --git a/quote_generator/js/scripts.js b/quote_generator/js/scripts.js index 1163ae0..ab23e11 100644 --- a/quote_generator/js/scripts.js +++ b/quote_generator/js/scripts.js @@ -1,7 +1,7 @@ -let darkModeIcon = document.querySelector('#dark-mode-icon'); +const darkModeIcon = document.querySelector('#dark-mode-icon'); darkModeIcon.onclick = () => { darkModeIcon.classList.toggle('bx-sun'); document.body.classList.toggle('dark-mode'); }; - \ No newline at end of file + diff --git a/scripts.js b/scripts.js index 954af5a..d856735 100644 --- a/scripts.js +++ b/scripts.js @@ -14,14 +14,14 @@ async function get_generator_card_footer() { const repoOwner = 'LifeAdventurer'; const repoName = 'generators'; for (let folderIndex = 1; folderIndex <= folderPaths.length; folderIndex++) { - let folderPath = folderPaths[folderIndex - 1]; + const folderPath = folderPaths[folderIndex - 1]; const apiUrl = `https://api.github.com/repos/${repoOwner}/${repoName}/commits?path=${folderPath}`; fetch(apiUrl) .then(response => response.json()) .then(data => { // the latest commit will be at the top of the list - let lastCommit = data[0].commit.author.date; + const lastCommit = data[0].commit.author.date; const commitTimeStamp = new Date(lastCommit).getTime() / 1000; const currentTimeStamp = Math.floor(new Date().getTime() / 1000); const timeDifference = currentTimeStamp - commitTimeStamp; @@ -51,9 +51,9 @@ function format_time_difference(seconds) { get_generator_card_footer() -let darkModeIcon = document.querySelector('#dark-mode-icon'); +const darkModeIcon = document.querySelector('#dark-mode-icon'); darkModeIcon.onclick = () => { darkModeIcon.classList.toggle('bx-sun'); document.body.classList.toggle('dark-mode'); -}; \ No newline at end of file +};