Refactor, last implementation was kind of stupid
This commit is contained in:
parent
51d6978124
commit
203b1015a9
32
index.mjs
32
index.mjs
|
@ -4,33 +4,27 @@ import fetch from "node-fetch";
|
|||
const APIURI = "https://fi.jamix.cloud/apps/menuservice/rest/haku/menu/92225/1?lang=fi";
|
||||
const myArgs = process.argv.slice(2);
|
||||
|
||||
function getDate() {
|
||||
switch (myArgs[0]) {
|
||||
case "t":
|
||||
return new Date(new Date().getTime() + 24 * 60 * 60 * 1000).toISOString().slice(0, 10).replaceAll('-', '');
|
||||
default:
|
||||
return new Date().toISOString().slice(0, 10).replaceAll('-', '');
|
||||
}
|
||||
}
|
||||
|
||||
function getFood(data) {
|
||||
function getAllFoods(data) {
|
||||
const days = data[0].menuTypes[0].menus[0].days
|
||||
const daysFiltered = days.filter((day) => String(day.date) === getDate());
|
||||
|
||||
const foods = [];
|
||||
foods.push(String(daysFiltered[0].date));
|
||||
daysFiltered[0].mealoptions.map((mealoption) => {
|
||||
let str = ""
|
||||
str += mealoption.name + ": "
|
||||
mealoption.menuItems.map((menuItem, index) => {
|
||||
index !== mealoption.menuItems.length - 1 ? str += menuItem.name + ", " : str += menuItem.name;
|
||||
days.map((day) => {
|
||||
foods.push(String(day.date));
|
||||
day.mealoptions.map((mealoption, index) => {
|
||||
let str = ""
|
||||
str += mealoption.name + ": "
|
||||
mealoption.menuItems.map((menuItem, index) => {
|
||||
index !== mealoption.menuItems.length - 1 ? str += menuItem.name + ", " : str += menuItem.name;
|
||||
})
|
||||
foods.push(str);
|
||||
index === day.mealoptions.length - 1 ? foods.push("\n") : '';
|
||||
})
|
||||
foods.push(str);
|
||||
})
|
||||
|
||||
|
||||
return foods;
|
||||
}
|
||||
|
||||
fetch(APIURI)
|
||||
.then((response) => response.json())
|
||||
.then((data) => getFood(data).map((food) => console.log(food)));
|
||||
.then((data) => getAllFoods(data).map(food => console.log(food)));
|
||||
|
|
Loading…
Reference in New Issue