-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.js
45 lines (43 loc) · 1.12 KB
/
state.js
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
let state = {};
const recipes = require("./recipes.json");
let categories = recipes
.map((r) => r.categories)
.flat()
.filter((v, i, a) => a.indexOf(v) === i)
.filter((cat) => !!cat);
let category_to_icon = require("./icons.json");
state.recipe_icons = (recipe) => {
if (!recipe.categories) {
return "";
}
let icons = [];
for (let category of recipe.categories) {
if (category in category_to_icon) icons.push(category_to_icon[category]);
}
return icons.join();
};
state.category_icon = (category) => {
if (category in category_to_icon) {
return category_to_icon[category];
}
return "";
};
// @ts-ignore
categories.sort((a, b) => (a > b ? 1 : -1));
let fav_idx = categories.findIndex((c) => c == "Favorite");
if (fav_idx > -1) {
categories.splice(fav_idx, 1);
categories.unshift("Favorite");
}
recipes.sort((a, b) => (a.name > b.name ? 1 : -1));
state.recipes = recipes;
state.categories = categories;
state.url_from = (name) =>
name
.toLowerCase()
.trim()
.replaceAll(" ", "-")
.replaceAll("/", "-")
.replaceAll("\\", "-")
.replaceAll(/-+/g, "-");
module.exports = state;