-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
39 lines (36 loc) · 1.03 KB
/
helpers.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
// algorithm from https://stackoverflow.com/a/12646864
export const shuffleArray = (array) => {
const copy = [...array];
for (let i = copy.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[copy[i], copy[j]] = [copy[j], copy[i]];
}
return copy;
};
const filterIslands = (countries) => {
const noIslands = countries.filter(
(i) => i.name.common.toLowerCase().match(/land|territor/) === null
);
return noIslands;
};
export const getCountries = async (lang) => {
const res = await fetch("https://restcountries.com/v3.1/all");
const data = await res.json();
const countries = filterIslands(data);
console.log(countries.length);
// english names are in country.name.common for some reason
if (!lang) {
return countries.map((country) => {
return {
name: country.name.common,
flagSrc: country.flags.svg,
};
});
}
return countries.map((country) => {
return {
name: country.translations[lang].common,
flagSrc: country.flags.svg,
};
});
};