From 6bf95f889c8933a18789d043cd20359e00de075b Mon Sep 17 00:00:00 2001 From: Starfall Date: Mon, 16 Oct 2023 15:02:53 -0500 Subject: fix moon phase bug with a lot of refactoring --- js/moon-phase.js | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/js/moon-phase.js b/js/moon-phase.js index 5511bca..9554597 100644 --- a/js/moon-phase.js +++ b/js/moon-phase.js @@ -1,8 +1,6 @@ // truncated to the day, America/Chicago time // only includes moons through end of 2024 (brown lunation number 1262) moons = [ -19585, 19593, 19599, 19606, -19614, 19622, 19629, 19636, 19644, 19651, 19658, 19666, 19674, 19681, 19688, 19695, 19703, 19710, 19717, 19725, @@ -20,29 +18,32 @@ moons = [ 20058, 20065, 20072, 20079, 20087, 20094, 20101, 20109 ] -icons = ['\u{1f311}', '\u{1f313}', '\u{1f315}', '\u{1f317}'] -icons_inverted = ['\u{1f315}', '\u{1f317}', '\u{1f311}', '\u{1f313}'] +icons = ['\u{1f311}\ufe0e', '\u{1f313}\ufe0e', '\u{1f315}\ufe0e', '\u{1f317}\ufe0e'] +// icons_inverted = ['\u{1f315}', '\u{1f317}', '\u{1f311}', '\u{1f313}'] -function getMoonsString(disable_emoji, inverted_icons) { - today = Math.floor((Date.now() - 18e6) / 86.4e6) - - next_index = moons.findIndex((element) => element >= today) - next_days = moons.slice(next_index, next_index + 4) +function getMoonsString(date) { + next_phase = moons.findIndex((e) => e >= date) + next_four_phases = moons.slice(next_phase, next_phase + 4) str = "" - for (let i = today; i < today + 30; i++) { - j = next_days.indexOf(i) + 1 - if(j != 0) { - icon = disable_emoji && inverted_icons ? icons_inverted[j%4] : icons[j%4] - str = str.concat(icon) - if(disable_emoji) - str = str.concat('\ufe0e') - } - else str = str.concat('\u2500') - } + + // unrolled loop just to git er done + str = str.concat('\u2500'.repeat(next_four_phases[0] - date)) + .concat(icons[next_phase % 4]) + + .concat('\u2500'.repeat(next_four_phases[1] - next_four_phases[0])) + .concat(icons[(next_phase + 1) % 4]) + + .concat('\u2500'.repeat(next_four_phases[2] - next_four_phases[1])) + .concat(icons[(next_phase + 2) % 4]) + + .concat('\u2500'.repeat(next_four_phases[3] - next_four_phases[2])) + .concat(icons[(next_phase + 3) % 4]) + return str } document.addEventListener('DOMContentLoaded', (event) => { - document.getElementById('decor-moons').innerHTML = getMoonsString(true, false) + today = Math.floor((Date.now() - 18e6) / 86.4e6) + document.getElementById('decor-moons').innerHTML = getMoonsString(today) }) -- cgit