-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
update-countries.js
59 lines (57 loc) · 1.76 KB
/
update-countries.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
53
54
55
56
57
58
59
const fs = require("fs");
const axios = require("axios");
axios("https://static.openfoodfacts.org/data/taxonomies/countries.json")
.then(({ data }) => {
fs.writeFile(
"./src/assets/countries.json",
JSON.stringify(
Object.entries(data)
.map(([key, value]) => {
let countryCode =
value.country_code_2 === undefined
? undefined
: value.country_code_2.en.toLowerCase();
if (countryCode === "world") {
countryCode = "";
}
const languageCode =
value.language_codes === undefined
? "en"
: value.language_codes.en === undefined
? undefined
: value.language_codes.en.split(",")[0];
return {
id: key,
label: value.name.en,
languageCode,
countryCode,
};
})
.filter((country) => country.countryCode !== undefined)
.sort((a, b) => a.label.localeCompare(b.label)),
),
() => console.log("Countries updated"),
);
fs.writeFile(
"./src/assets/languages.json",
JSON.stringify(
[
...new Set(
Object.values(data)
.map((value) => {
const languageCode =
value.language_codes === undefined
? "en"
: value.language_codes.en === undefined
? undefined
: value.language_codes.en.split(",")[0];
return languageCode;
})
.filter(Boolean),
),
].sort(),
),
() => console.log("Countries updated"),
);
})
.catch(console.error);