From d12f0543274b83b20004319a34c215212a6924c3 Mon Sep 17 00:00:00 2001 From: yasuomaidana Date: Sun, 13 Jun 2021 18:39:34 -0500 Subject: [PATCH 1/4] get country details by range of size added --- index.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d831d02..503c234 100644 --- a/index.js +++ b/index.js @@ -265,7 +265,21 @@ 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 +*/ +const getCountriesByRangeSize =(areaLower,areaUpper,sys="km")=>{ + if(sys!="sys"&&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 +297,5 @@ module.exports = { getCountriesByConstitutionalForm, getCountriesByLandLock, getCountryNeighbors, + getCountriesByRangeSize }; From 7486c47515f7a5b04ea4c80aaad0ebc3b196037b Mon Sep 17 00:00:00 2001 From: yasuomaidana Date: Sun, 13 Jun 2021 18:40:25 -0500 Subject: [PATCH 2/4] get country details by range of size added --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 503c234..117acea 100644 --- a/index.js +++ b/index.js @@ -271,7 +271,7 @@ const getCountryNeighbors = (country) => { * @param { Number } areaLower Minimum area * @param { Number } areaUpper Maximum area * @param {sys} Area system kilometers^2 = km, miles^2 = mi - * @returns + * @returns {Country[]} An array of country objects */ const getCountriesByRangeSize =(areaLower,areaUpper,sys="km")=>{ if(sys!="sys"&&sys!="mi"){sys="km";} From 212d50fd67434e1f7ee48137c464f41512dd1bb9 Mon Sep 17 00:00:00 2001 From: yasuomaidana Date: Sun, 13 Jun 2021 18:41:20 -0500 Subject: [PATCH 3/4] test of get country details by range of size created --- spec/getCountriesSpec.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/getCountriesSpec.js b/spec/getCountriesSpec.js index 1cb6797..f28f6fa 100644 --- a/spec/getCountriesSpec.js +++ b/spec/getCountriesSpec.js @@ -271,4 +271,17 @@ 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); + }); + }) }); From ca90f5db2a24966043b6e2e47c542dffdc7d702f Mon Sep 17 00:00:00 2001 From: yasuomaidana Date: Sun, 13 Jun 2021 23:59:08 -0500 Subject: [PATCH 4/4] sys input bug fixed --- index.js | 3 ++- spec/getCountriesSpec.js | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 117acea..504395e 100644 --- a/index.js +++ b/index.js @@ -274,7 +274,8 @@ const getCountryNeighbors = (country) => { * @returns {Country[]} An array of country objects */ const getCountriesByRangeSize =(areaLower,areaUpper,sys="km")=>{ - if(sys!="sys"&&sys!="mi"){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; }); diff --git a/spec/getCountriesSpec.js b/spec/getCountriesSpec.js index f28f6fa..98e2eb6 100644 --- a/spec/getCountriesSpec.js +++ b/spec/getCountriesSpec.js @@ -283,5 +283,14 @@ describe("The index", () => { 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); + }); }) });