Skip to content
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
18 changes: 17 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,22 @@ const getCountryNeighbors = (country) => {

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

/**
* Returns an array of objects containing all countries
* which areas satisfies areaLower<=Country Area<=areaUpper
* @param { Number } areaLower Minimum area
* @param { Number } areaUpper Maximum area
* @param {sys} Area system kilometers^2 = km, miles^2 = mi
* @returns {Country[]} An array of country objects
*/
const getCountriesByRangeSize =(areaLower,areaUpper,sys="km")=>{
sys=sys.toLowerCase();
if(sys!="km"&&sys!="mi"){sys="km";}
let countries = data.filter(country=>{
return country.area[sys+"2"]>=areaLower && country.area[sys+"2"]<=areaUpper;
Comment thread
yasuomaidana marked this conversation as resolved.
});
return countries;
}
module.exports = {
getRandomCountry,
getNRandomCountriesData,
Expand All @@ -283,4 +298,5 @@ module.exports = {
getCountriesByConstitutionalForm,
getCountriesByLandLock,
getCountryNeighbors,
getCountriesByRangeSize
};
22 changes: 22 additions & 0 deletions spec/getCountriesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,26 @@ describe("The index", () => {
expect(() => countryApi.getCountryNeighbors('non-existing country')).toThrow();
})
});

describe("To get getCountriesByRangeSize",()=>{
const expectedPLNeighborCountries = [
...countryApi.getCountryDetailsByName('afghanistan'),
...countryApi.getCountryDetailsByName('myanmar')
];
it("returns countries that area is between 652864 and 700000 km2", () => {
expect(countryApi.getCountriesByRangeSize(652864,700000)).toEqual(expectedPLNeighborCountries);
});
it("returns countries that area is between 252072 and 270271 mi2", () => {
expect(countryApi.getCountriesByRangeSize(252072,270271,"mi")).toEqual(expectedPLNeighborCountries);
});
it("returns countries that area is between 652864 and 700000 and sys = KM", () => {
expect(countryApi.getCountriesByRangeSize(652864,700000,"KM")).toEqual(expectedPLNeighborCountries);
});
it("returns countries that area is between 252072 and 270271 and sys = MI", () => {
expect(countryApi.getCountriesByRangeSize(252072,270271,"MI")).toEqual(expectedPLNeighborCountries);
});
it("returns countries that area is between 652864 and 700000 and sys = whatever", () => {
expect(countryApi.getCountriesByRangeSize(652864,700000,"whatever")).toEqual(expectedPLNeighborCountries);
});
})
});