-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.js
286 lines (257 loc) · 8.79 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
let data = require("./data/data.json");
const randomNum = () => {
return Math.floor(Math.random() * data.length);
};
/**
* Returns the name of a random country from the list of
* countries in the dataset
* @returns {string} The name of a country picked at random
*/
const getRandomCountry = () => {
let randNum = randomNum();
return data[randNum].country;
};
/**
* Returns an array having `count` number of different random country objects,
* each object containing `country`, `capital`, `currency`, `native_language`,
* `famous_for`, and `phone_code`.
* @param {integer} count Number of country objects in the array to be returned
* @returns {Array} An array having `count` number of country objects
*/
const getNRandomCountriesData = (count) => {
let randomCountriesSet = new Set(); // to prevent duplicate countries
while (randomCountriesSet.size < count) {
let country = data[randomNum()];
randomCountriesSet.add(country); // adds a country to the Array
}
return Array.from(randomCountriesSet); // Returns the Array
};
// Helper function
const getCountriesByObject = (value, obj) => {
let resultArray = [];
value = value.toLowerCase();
object = obj;
data.forEach((item) => {
item[object] = item[object];
if (item[object] && item[object].includes(value)) {
resultArray.push(item);
}
});
return resultArray;
};
/**
* Returns an array of objects, each containing `country`, `capital`,
* `currency` and `native_language` filtered by `capital` .
* @param {string} capital The name (not case-sensitive) of the capital of the country
* @returns {Array} An array of country objects
*/
const getCountryDetailsByCapital = (capital) => {
return getCountriesByObject(capital, "capital");
};
/**
* Returns an array of objects, each containing `country`, `capital`,
* `currency` and `native_language` filtered by `country` .
* @param {string} country The name (not case-sensitive) of the country
* @returns {Array} An array of country objects
*/
const getCountryDetailsByName = (country) => {
return getCountriesByObject(country, "country");
};
/**
* Returns an array of objects, each containing `country`, `capital`,
* `currency` and `native_language` filtered by the `languageSpoken` .
* @param {string} languageSpoken The language spoken (not case-sensitive) by the country
* @returns {Array} An array of country objects
*/
const getCountriesByLanguage = (languageSpoken) => {
return getCountriesByObject(languageSpoken, "native_language");
};
/**
* Returns an array of objects, each containing `country`, `capital`,
* `currency` and `native_language` filtered by the `languageSpoken` .
* @param {'left' | 'right'} direction The driving direction followed by the country
* @returns {Array} An array of country objects
*/
const getCountriesByDriveDirection = (direction) => {
let value;
switch (direction) {
case "left":
value = "left";
break;
case "right":
value = "right";
break;
default:
throw new Error('direction must be "left" or "right"');
}
return getCountriesByObject(value, "drive_direction");
};
/**
* Returns an array of objects containing all countries, each containing `country`, `capital`,
* `currency`, `native_language`, `famous_for`, and `phone_code`
* @returns {Array} An array of country objects
*/
const getAllCountryDetails = () => {
return data;
};
/**
* Returns an array containing the name of all the countries in the dataset
* @returns {Array} An array of country objects
*/
const getAllCountries = () => {
return data.map(({ country }) => country);
};
/**
* Returns an array of objects containing all countries, each containing `country`, `capital`,
* `currency`, `native_language`, `famous_for`, and `phone_code`, filtered by `famousThing`
* @param {string} famousThing What the country is famous for
* @returns {Array} An array of country objects
*/
const getCountriesByFamousFor = (famousThing) => {
return data.filter(
(country) => country.famous_for.search("\\b" + famousThing + "\\b") != -1
);
};
const getCountriesByAlcoholProhibition = (prohibitionType) =>{
let value;
switch (prohibitionType) {
case "none":
value = "none";
break;
case "limited":
value = "limited";
break;
case "regional":
value = "regional";
break;
case "nationwide":
value = "nationwide";
break;
default:
throw new Error('Prohibition type must be "none", "limited", "regional" or "nationwide"');
}
return getCountriesByObject(value,"alcohol_prohibition");
}
/**
* Returns an array of objects, each containing `country`, `capital`, `currency`, `native_language`,
* `famous_for`, `phone_code`, `flag`, `drive_direction`, `continent`, `iso` and `tld`
* filtered by `continentCode`
* @param {string} continentCode The continent 2-letter code (not case-sensitive)
* @returns {Array} An array of country objects
*/
const getCountriesByContinent = (continentCode) => {
continentCode = continentCode.toLowerCase();
return data.filter(country => country.continent
.split("/")
.includes(continentCode)
);
};
/**
* Returns an array with object containing `country`, `capital`, `currency`, `native_language`,
* `famous_for`, `phone_code`, `flag`, `drive_direction`, `continent`, `iso` and `tld`
* filtered by specific `iso`
* @param {'numeric' | 'alpha_2' | 'alpha_3'} isoType The code of the country (ISO 3166-1 standard)
* @param {string} isoValue The ISO code value (not case-sensitive) of the country
* @returns {Array} An array with country object
*/
const getCountryDetailsByISO = (isoType, isoValue) => {
let type;
isoType = isoType.toLowerCase();
isoValue = isoValue.toLowerCase();
switch (isoType) {
case "numeric":
type = "numeric";
break;
case "alpha_2":
type = "alpha_2";
break;
case "alpha_3":
type = "alpha_3";
break;
default:
throw new Error("isoType must be 'numeric', 'alpha_2' or 'alpha_3'");
}
return data.filter(country => country.iso[type] === isoValue);
};
/**
* Returns an array of objects containing `country`, `capital`, `currency`, `native_language`,
* `famous_for`, `phone_code`, `flag`, `drive_direction`, `continent`, `iso` and `tld`
* filtered by `tld`
* @param {string} tldName The name (not case-sensitive) of the country code top-level domain
* @returns {Array} An array of country objects
*/
const getCountriesByTLD = (tldName) => {
tldName = tldName.toLowerCase();
return data.filter(country => country.tld
.split("/")
.includes(tldName)
);
};
/**
* Returns an array of objects containing all countries, each containing `country`, `capital`,
* `currency`, `native_language`, `famous_for`, and `phone_code`, `flag`, `drive_direction`
* and `constitutional_form` filtered by `constitutional_form`
* @param {string} constitutionalFormName Name of country constitutional form
* @returns {Array} An array of country objects
*/
const getCountriesByConstitutionalForm = (constitutionalFormName) => {
const result = data.filter((country) => {
return country.constitutional_form.includes(constitutionalFormName)
});
if (!result.length) {
throw new Error(
`No country was found! Available constitutional forms are:
'republic', 'constitutional monarchy', 'absolute monarchy' and 'n/a'
`);
}
return result;
}
/**
* Returns an array of objects containing all countries, each containing `country`, `capital`,
* `currency`, `native_language`, `famous_for`, `phone_code`, `flag` and `drive_direction` filtered by `is_landlocked`
* @param { Boolean } isLandLocked Country that is surrounded by one or more countries
* @returns {Array} An array of country objects
*/
const getCountriesByLandLock = (isLandLocked) => {
return data.filter( country => country.is_landlocked === isLandLocked);
};
/**
* Get list of neighbor countries
* @param {String} country - name (or one of ISO 3166-1 code) of country
* @returns {Array}
*/
const getCountryNeighbors = (country) => {
const foundCountry = data.find((item) => {
switch (country.toLowerCase()) {
case item.country:
case item.iso.numeric:
case item.iso.alpha_2:
case item.iso.alpha_3:
return true
default:
return false;
}
});
if (!foundCountry) {
throw new Error(`Country '${country}' was not found!`);
}
return data.filter(({ neighbors }) => neighbors.includes(foundCountry.iso.alpha_2));
};
module.exports = {
getRandomCountry,
getNRandomCountriesData,
getCountryDetailsByCapital,
getCountryDetailsByName,
getAllCountryDetails,
getAllCountries,
getCountriesByLanguage,
getCountriesByFamousFor,
getCountriesByDriveDirection,
getCountriesByAlcoholProhibition,
getCountriesByContinent,
getCountryDetailsByISO,
getCountriesByTLD,
getCountriesByConstitutionalForm,
getCountriesByLandLock,
getCountryNeighbors,
};