From 203b1015a971d58de26614a43cfd20b333af4bb1 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 14 Nov 2022 10:41:50 +0200 Subject: [PATCH] Refactor, last implementation was kind of stupid --- index.mjs | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/index.mjs b/index.mjs index 5fd6e4a..66d0cdf 100755 --- a/index.mjs +++ b/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)));