forked from bhatvikrant/world-countries-capitals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (125 loc) · 4.25 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
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
);
};
module.exports = {
getRandomCountry,
getNRandomCountriesData,
getCountryDetailsByCapital,
getCountryDetailsByName,
getAllCountryDetails,
getAllCountries,
getCountriesByLanguage,
getCountriesByFamousFor,
getCountriesByDriveDirection,
};