diff options
author | JoelHMikael <joel.h.kronqvist@gmail.com> | 2021-12-09 08:19:20 +0200 |
---|---|---|
committer | JoelHMikael <joel.h.kronqvist@gmail.com> | 2021-12-09 08:19:20 +0200 |
commit | 1398a39779132974812ff1336f4af8e51b8e92cf (patch) | |
tree | 6eeef9b479c6ae4dc6c3049e3463273bbae28169 /server.js | |
parent | 7a02701c6963cddeceb894aec373e4866cfdaa2d (diff) | |
download | LYLLRuoka-1398a39779132974812ff1336f4af8e51b8e92cf.tar.gz LYLLRuoka-1398a39779132974812ff1336f4af8e51b8e92cf.zip |
Readability improvements
buildMain in server.js
Diffstat (limited to 'server.js')
-rw-r--r-- | server.js | 45 |
1 files changed, 27 insertions, 18 deletions
@@ -69,34 +69,35 @@ async function buildMain(args) if (typeof query.index === "string") index = query.index.toUpperCase().replaceAll(".", "").replaceAll(" ", ""); const DB = args["db"]; - const data = await openFile(path); let data_string = data.toString("utf-8"); - let res; + let res = {}; + const d = new Date(); let day = d.getDay(); - if ((typeof query.day === "string") && (parseInt(query.day).toString() === query.day) && (!isNaN(parseInt(query.day))) && (parseInt(query.day) > 0) && (parseInt(query.day) < 7)) day = parseInt(query.day); + data_string = data_string.replace(`<option value=\"${day}\">`, `<option value=\"${day}\" selected>`); + // get the food shift to res["shift"] + res["shift"] = undefined; if ((day === 0) || (day === 6)) - res = `Maanantain ruoka: ${parse.get(day, query.index, DB)}`; + res["shift"] = `Maanantain ruoka: ${parse.get(day, query.index, DB)}`; if ((index === undefined) || (index === "")) - res = ""; - if (res === undefined) - res = parse.get(day, index, DB); - if (res === -1) - res = "Kyseiselle kurssille/opettajalle ei löydy ruokailua päivältä!"; // it's the frickin \r in the database! - data_string = data_string.replace("\\(result\\)", res); - data_string = data_string.replace(`<option value=\"${day}\">`, `<option value=\"${day}\" selected>`); - - day = ["su", "ma", "ti", "ke", "to", "pe", "la"][day]; - if (res !== "") - data_string = data_string.replace("\\(foodshift-header\\)", `Kurssin (???)/(???) ruokailuvuoro ${day}:`); - else + res["shift"] = ""; + if (res["shift"] === undefined) + res["shift"] = parse.get(day, index, DB); + if (res["shift"] === -1) + res["shift"] = "Kyseiselle kurssille/opettajalle ei löydy ruokailua päivältä!"; + + // get the day + res["foodshift-header"] = `Kurssin (???)/(???) ruokailuvuoro ${["su", "ma", "ti", "ke", "to", "pe", "la"][day]}:` + if (res["shift"] === "") data_string = data_string.replace('<div id="shift-result" class="float-block">', '<div id="shift-result" class="float-block" style="display: none;">'); + data_string = build_replace(data_string, res); + return data_string; } @@ -114,9 +115,17 @@ async function buildDefault(args) return data.toString("utf-8"); } -function parseshift(index) + +function build_replace(s, dict) { - return 1; // placeholder glue + console.log(dict); + for (const [key, val] of Object.entries(dict)) + { + console.log(`\\(${key}\\)`); + s = s.replaceAll(`\\(${key}\\)`, val); + } + + return s; } |