diff --git a/index.js b/index.js index d831d02..504395e 100644 --- a/index.js +++ b/index.js @@ -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; + }); + return countries; +} module.exports = { getRandomCountry, getNRandomCountriesData, @@ -283,4 +298,5 @@ module.exports = { getCountriesByConstitutionalForm, getCountriesByLandLock, getCountryNeighbors, + getCountriesByRangeSize }; diff --git a/spec/getCountriesSpec.js b/spec/getCountriesSpec.js index 1cb6797..98e2eb6 100644 --- a/spec/getCountriesSpec.js +++ b/spec/getCountriesSpec.js @@ -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); + }); + }) });