1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
export const parseLunaSpeiseplan = (pages: Array<string[]>) => {
const daynames = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag"];
const menuOptions = ["A", "B"];
const currentYear = new Date().getFullYear().toString();
const groupMenues = (page: string[]) => {
const menueGroups = [];
let currentDays = [];
let currentMenuOption = "";
let currentDayIndex = -1;
page.forEach((line) => {
if (
daynames.some((dayname) => line.includes(dayname)) &&
line.includes(currentYear)
) {
currentDays = line.split(currentYear).map((e) => e.trim()).filter((e) =>
e !== ""
).map((e) => ({
when: e,
options: {},
}));
// console.log(currentDays);
menueGroups.push(currentDays);
currentMenuOption = "";
}
else if (menuOptions.includes(line)) {
currentDayIndex = currentMenuOption !== line ? 0 : currentDayIndex + 1;
currentMenuOption = line;
}
else {
const tokensToRemove = [
"m",
"gw",
"gg",
"gr",
"sl",
"sm",
"sc",
"lu",
"sj",
"gh",
"e",
"g",
"gd",
"n",
"sn",
]
.map((token) => new RegExp(`( ${token})($| |\\()`, "g"));
let cleanedMenuText = line;
tokensToRemove.forEach((removeToken) => {
cleanedMenuText = cleanedMenuText.replace(removeToken, "$2");
});
const currentMenuText =
currentDays[currentDayIndex].options[currentMenuOption] || "";
currentDays[currentDayIndex].options[currentMenuOption] =
(currentMenuText + " " + cleanedMenuText).trim();
}
});
return menueGroups;
};
const ignorableTexts = [
"Speiseplan HXB",
"Alle grünen Komponenten sind gem. der VO (EG) VO 2018/848 zu 100% aus Bio-Zutaten",
"Änderungen vorbehalten!",
];
const lineFilter = (line) => {
const noDessertFilter = (line) =>
!(line.includes("/") &&
["Obst", "Gemüse", "Salat"].some((token) => line.includes(token)));
return !ignorableTexts.includes(line) && noDessertFilter(line);
};
const cleanedPages = pages.map((page) => {
return page.filter((line) => lineFilter(line));
});
const groupedMenues = cleanedPages.map((page) => groupMenues(page)).flat(2);
return groupedMenues;
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023