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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,16 @@ wcc.getCountryNeighbors(country)
// Example: wcc.getCountryNeighbors('poland')
```

```js
/**
* Get list of countries by phoneCode
* @param {String} phoneCode - +91, +1 etc..
* @returns {Country[]}
*/
wcc.getCountryByPhoneCode(phoneCode)
// Example: wcc.getCountryByPhoneCode('+91')
```

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

Expand Down
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,20 @@ const getCountryNeighbors = (country) => {
return data.filter(({ neighbors }) => neighbors.includes(foundCountry.iso.alpha_2));
};

/**
* Get list of countries based on phone code
* @param {String} phoneCode - +91, +1 etc..
* @returns {Array}
*/
const getCountryByPhoneCode = function (phoneCode) {
if (getCountryByPhoneCode.length !== arguments.length) {
throw new Error("phoneCode is required");
} else if (typeof phoneCode !== "string") {
throw new Error("phoneCode must be a string");
}
return data.filter((eachCountry) => eachCountry.phone_code === phoneCode);
};

module.exports = {
getRandomCountry,
getNRandomCountriesData,
Expand All @@ -283,4 +297,5 @@ module.exports = {
getCountriesByConstitutionalForm,
getCountriesByLandLock,
getCountryNeighbors,
getCountryByPhoneCode,
};
27 changes: 27 additions & 0 deletions spec/getCountriesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,31 @@ describe("The index", () => {
expect(() => countryApi.getCountryNeighbors('non-existing country')).toThrow();
})
});

describe("The getCountryByPhoneCode", () => {
const expectedCountryWithPhoneCode91 =
countryApi.getCountryByPhoneCode("+91");
const expectCountryWithPhoneCode1 = countryApi.getCountryByPhoneCode("+1");

it("returns countries where phone_code is '+91' ", () => {
expect(countryApi.getCountryByPhoneCode("+91")).toEqual(
expectedCountryWithPhoneCode91
);
});

it("returns countries where phone_code is '+1' ", () => {
expect(countryApi.getCountryByPhoneCode("+1")).toEqual(
expectCountryWithPhoneCode1
);
});

it("throws an error if passed argument(phoneCode) not a string", () => {
expect(() => countryApi.getCountryByPhoneCode(12)).toThrow();
});

it("throws an error if argument(phoneCode) not passed", () => {
expect(() => countryApi.getCountryByPhoneCode()).toThrow();
});
});

});