Skip to content
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

Get countries by continent #180

Open
wants to merge 5 commits into
base: develop
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

166 changes: 166 additions & 0 deletions src/Continents/Continents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
const africanCountries = [
{
name: 'Algeria',
},
{
name: 'Angola',
},
{
name: 'Benin',
},
{
name: 'Botswana',
},
{
name: 'Burkina Faso',
},
{
name: 'Burundi',
},
{
name: 'Cabo Verde',
},
{
name: 'Cameroon',
},
{
name: 'Central African Republic',
},
{
name: 'Chad',
},
{
name: 'Comoros',
},
{
name: 'Democratic Republic of the Congo',
},
{
name: 'Republic of the Congo',
},
{
name: 'Djibouti',
},
{
name: 'Egypt',
},
{
name: 'Equatorial Guinea',
},
{
name: 'Eritrea',
},
{
name: 'Eswatini',
},
{
name: 'Ethiopia',
},
{
name: 'Gabon',
},
{
name: 'Gambia',
},
{
name: 'Ghana',
},
{
name: 'Guinea',
},
{
name: 'Guinea-Bissau',
},
{
name: 'Ivory Coast',
},
{
name: 'Kenya',
},
{
name: 'Lesotho',
},
{
name: 'Liberia',
},
{
name: 'Libya',
},
{
name: 'Madagascar',
},
{
name: 'Malawi',
},
{
name: 'Mali',
},
{
name: 'Mauritania',
},
{
name: 'Mauritius',
},
{
name: 'Morocco',
},
{
name: 'Mozambique',
},
{
name: 'Namibia',
},
{
name: 'Niger',
},
{
name: 'Nigeria',
},
{
name: 'Rwanda',
},
{
name: 'Sao Tome and Principe',
},
{
name: 'Senegal',
},
{
name: 'Seychelles',
},
{
name: 'Sierra Leone',
},
{
name: 'Somalia',
},
{
name: 'South Africa',
},
{
name: 'South Sudan',
},
{
name: 'Sudan',
},
{
name: 'Tanzania',
},
{
name: 'Togo',
},
{
name: 'Tunisia',
},
{
name: 'Uganda',
},
{
name: 'Zambia',
},
{
name: 'Zimbabwe',
},
];

export default africanCountries;
28 changes: 28 additions & 0 deletions src/__test__/country.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,34 @@ const executeAllTests = (Country: any) => {
const country: ICountry = Country.getCountryByCode(code);
expect(country).toEqual(code);
});

test('Check for get all african countries', () => {
const africans = Country.getAllAfricanCountries();
expect(africans[0].name).toEqual('Algeria');
});

test('Check for get all african countries', () => {
const africans = Country.getAllAfricanCountries();
const firstCountry = {
isoCode: 'DZ',
name: 'Algeria',
phonecode: '213',
flag: '🇩🇿',
currency: 'DZD',
latitude: '28.00000000',
longitude: '3.00000000',
timezones: [
{
zoneName: 'Africa/Algiers',
gmtOffset: 3600,
gmtOffsetName: 'UTC+01:00',
abbreviation: 'CET',
tzName: 'Central European Time',
},
],
};
expect(africans[0]).toEqual(firstCountry);
});
});
};

Expand Down
12 changes: 12 additions & 0 deletions src/country.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import countryList from './assets/country.json';
import { compare, findEntryByCode } from './utils';
import { ICountry } from './interface';
import africanCountries from './Continents/Continents';

// Get a country by isoCode.
function getCountryByCode(isoCode: string): ICountry | undefined {
Expand All @@ -14,6 +15,16 @@ function getAllCountries(): ICountry[] {
return countryList;
}

// Get a list of all african countries.
function getAllAfricanCountries(): ICountry[] {
const africanCountryNames = africanCountries.map((country) => {
return country.name;
});
return countryList.filter((country) => {
return africanCountryNames.includes(country.name);
});
}

function sortByIsoCode(countries: ICountry[]): ICountry[] {
return countries.sort((a, b) => {
return compare<ICountry>(a, b, (entity) => {
Expand All @@ -26,4 +37,5 @@ export default {
getCountryByCode,
getAllCountries,
sortByIsoCode,
getAllAfricanCountries,
};
Loading