Skip to content

added function getAvailablePropertyNames (fixes iss #44) and rewrote function getNRandomCountries (fixes iss #70) and wrote function getCountryDetailsByPhoneCode (fixes iss #71) #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ wcc.getCountryNeighbors(country)
// Example: wcc.getCountryNeighbors('poland')
```

```js
/*
* Get a list of available parameters for each country
*/
wcc.getAvailablePropertyNames()
// Example: wcc.getAvailablePropertyNames()
```

❗️ All params are **NOT** case sensitive so no matter how argument looks,
the response will remain the same.

Expand Down
66 changes: 41 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ const getRandomCountry = () => {
* @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
// validate function input
if (count > data.length)
throw new Error(`there are only ${data.length} countries!`);
if (count <= 0) throw new Error(`number of countries must be positive.`);

let dataCopy = [...data];
dataCopy = dataCopy.sort(() => Math.random()); // shuffles the array

return dataCopy.slice(0, count); // return a slice of the shuffled array by the size 'count'
};

// Helper function
Expand Down Expand Up @@ -125,7 +128,7 @@ const getCountriesByFamousFor = (famousThing) => {
);
};

const getCountriesByAlcoholProhibition = (prohibitionType) =>{
const getCountriesByAlcoholProhibition = (prohibitionType) => {
let value;
switch (prohibitionType) {
case "none":
Expand All @@ -141,10 +144,12 @@ const getCountriesByAlcoholProhibition = (prohibitionType) =>{
value = "nationwide";
break;
default:
throw new Error('Prohibition type must be "none", "limited", "regional" or "nationwide"');
throw new Error(
'Prohibition type must be "none", "limited", "regional" or "nationwide"'
);
}
return getCountriesByObject(value,"alcohol_prohibition");
}
return getCountriesByObject(value, "alcohol_prohibition");
};

/**
* Returns an array of objects, each containing `country`, `capital`, `currency`, `native_language`,
Expand All @@ -156,9 +161,8 @@ const getCountriesByAlcoholProhibition = (prohibitionType) =>{
const getCountriesByContinent = (continentCode) => {
continentCode = continentCode.toLowerCase();

return data.filter(country => country.continent
.split("/")
.includes(continentCode)
return data.filter((country) =>
country.continent.split("/").includes(continentCode)
);
};

Expand Down Expand Up @@ -190,7 +194,7 @@ const getCountryDetailsByISO = (isoType, isoValue) => {
throw new Error("isoType must be 'numeric', 'alpha_2' or 'alpha_3'");
}

return data.filter(country => country.iso[type] === isoValue);
return data.filter((country) => country.iso[type] === isoValue);
};

/**
Expand All @@ -203,10 +207,7 @@ const getCountryDetailsByISO = (isoType, isoValue) => {
const getCountriesByTLD = (tldName) => {
tldName = tldName.toLowerCase();

return data.filter(country => country.tld
.split("/")
.includes(tldName)
);
return data.filter((country) => country.tld.split("/").includes(tldName));
};

/**
Expand All @@ -218,27 +219,28 @@ const getCountriesByTLD = (tldName) => {
*/
const getCountriesByConstitutionalForm = (constitutionalFormName) => {
const result = data.filter((country) => {
return country.constitutional_form.includes(constitutionalFormName)
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);
return data.filter((country) => country.is_landlocked === isLandLocked);
};

/**
Expand All @@ -253,7 +255,7 @@ const getCountryNeighbors = (country) => {
case item.iso.numeric:
case item.iso.alpha_2:
case item.iso.alpha_3:
return true
return true;
default:
return false;
}
Expand All @@ -263,7 +265,19 @@ const getCountryNeighbors = (country) => {
throw new Error(`Country '${country}' was not found!`);
}

return data.filter(({ neighbors }) => neighbors.includes(foundCountry.iso.alpha_2));
return data.filter(({ neighbors }) =>
neighbors.includes(foundCountry.iso.alpha_2)
);
};

const getAvailablePropertyNames = () =>
Object.keys(getCountryDetailsByName(getRandomCountry())[0]);

const getCountryDetailsByPhoneCode = (code) => {
for (let country of getAllCountryDetails()) {
if (country.phone_code === code) return country;
}
throw new Error("There are no countries with this phone code!");
};

module.exports = {
Expand All @@ -283,4 +297,6 @@ module.exports = {
getCountriesByConstitutionalForm,
getCountriesByLandLock,
getCountryNeighbors,
getAvailablePropertyNames,
getCountryDetailsByPhoneCode,
};
Loading