0.1.19 • Published 4 years ago

@saadixl/countries v0.1.19

Weekly downloads
13
License
MIT
Repository
github
Last release
4 years ago

countries

Get countries by country name, country code, currency, continent, capital.

How to install

$ npm install @saadixl/countries --save

or

$ yarn add @saadixl/countries

How to use

Include countries in your module

const countries = require('@saadixl/countries');

For getting info about one country

What can be passed as string parameter?

  • Country code, example: "BD"
  • Country name, example: "bangladesh"
  • ISO Country code, example: "SGP"
  • Capital name, example: "DhAka"

Note that the parameter is case insensitive

You can get information about a country by passing country code or country name or country iso code or capital as string parameter. For example, if you pass Bangladesh as a parameter like this:

countries("Bangladesh")

or you pass the country code of Bangladesh like:

countries("bd")

You will get the following object as result.

{ 
  capital: 'Dhaka',
  continentCode: 'AS',
  continentName: 'Asia',
  countryCodeISO: 'BGD',
  currencyCode: 'BDT',
  name: 'Bangladesh',
  names: [ 'Bangladesh', 'People Republic of Bangladesh' ],
  phoneCode: '880'
 }

Which means, it doesn't matter what format data you have an what format data you need, you will get it!

For getting a list of countries

What can be passed as config?

  • currency can be currency code, currency name
  • continent can be continent code, continent name

Note that you can pass both currency and continent together. At least one property as config is required.

Suppose, you need all the country codes of Europe or you need all the country names who uses USD. Firstly, you can pass a config object while calling countries. A config object can have currency and continent for now. Once you pass a config object, now you can call some methods like:

  • getAll()
  • getCountryCodes()
  • getISOCountryCodes()
  • getCountryNames()
  • getPhoneCodes()
  • getCapitals()

Or you can call getCapitals(property) where property can be any property inside the country object. For example if we really want to find all the country names who uses USD, we call:

countries({currency: "USD"})

It will return the following list:

[ 
  'American Samoa',
  'Bonaire, Sint Eustatius and Saba',
  'Ecuador',
  'Federated States of Micronesia',
  'Guam',
  'British Indian Ocean Territory',
  'Marshall Islands',
  'Northern Mariana Islands',
  'Puerto Rico',
  'Palau',
  'El Salvador',
  'Turks and Caicos Islands',
  'Timor-Leste',
  'United States Minor Outlying Islands',
  'United States of America',
  'British Virgin Islands',
  'Virgin Islands, U.S.' 
  ]

Just to make it little more interesting, we can try to find out all the country codes of the countries who uses USD as their currency but they are part of the Ocenia continent. To do that we call:

countries({currency: "USD", continent: "Oc"}).getCountryCodes()

It will return you the following array:

[ 'AS', 'FM', 'GU', 'MH', 'MP', 'PW', 'TL', 'UM' ]

I am planning to add region soon. I am developing this because I have not found everything in one place before. That's why taking things into own hands!

0.1.19

4 years ago

0.1.18

4 years ago

0.0.17

4 years ago

0.0.16

4 years ago

0.1.15

4 years ago

0.1.14

4 years ago

0.1.13

4 years ago

0.1.12

4 years ago

0.1.11

4 years ago