-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
update-nutriments.js
52 lines (49 loc) · 1.22 KB
/
update-nutriments.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
46
47
48
49
50
51
52
const fs = require("fs");
const axios = require("axios");
const idsToIgnore = [
"energy",
"fruits-vegetables-nuts",
"fruits-vegetables-nuts-dried",
"fruits-vegetables-nuts-estimate",
"carbon-footprint",
"carbon-footprint-from-meat-or-fish",
"nutrition-score-fr",
"nutrition-score-uk",
];
function parseNutrients(data, depth = 0) {
return data.flatMap((item) => {
const { display_in_edit_form, unit, id, name, nutrients } = item;
if (nutrients === undefined) {
return {
id,
name,
unit,
...(display_in_edit_form ? { display: true } : {}),
...(depth > 0 ? { depth } : {}),
};
}
return [
{
id,
name,
unit,
...(display_in_edit_form ? { display: true } : {}),
...(depth > 0 ? { depth } : {}),
},
...parseNutrients(nutrients, depth + 1),
];
});
}
axios("https://world.openfoodfacts.org/cgi/nutrients.pl")
.then(({ data }) => {
fs.writeFile(
"./src/assets/nutriments.json",
JSON.stringify(
parseNutrients(data.nutrients).filter(
({ id }) => !idsToIgnore.includes(id),
),
),
() => console.log("nutriments updated"),
);
})
.catch(console.error);