npm

Don't miss out on this year's npm Pride t-shirt designs!Order your favorites here »

dycountryjs

0.8.1 • Public • Published

dyCountryJS

Dictionary to get country info.

Status

license npm version Build Status

Getting Started

  • Download the latest release of the project.
  • Clone the repo: git clone https://github.com/yusufshakeel/dyCountryJS.git
  • Install via npm: npm install --save dycountryjs
  • Use from jsDelivr CDN: https://www.jsdelivr.com/package/npm/dycountryjs

Documentation

Check the index.html file of this project.

How to setup?

Run npm install to install all the packages.

Then run npm run all to build and run the tests.

You will get the minified file inside dist/js directory.

How to use?

Step 1: Include dyCountry.min.js file.

<!DOCTYPE html>
<html>
<head>
  <title>dyCountryJS Demo</title>
</head>
<body>
 
<!-- some html goes here -->
 
<!-- script -->
<script src="path/to/dyCountry.min.js"></script>
</body>
</html>

Step 2: Create an instance of dyCountry class and pass an optional config.

var config = {
  flagDir: 'path/to/dist/flags'
};
var obj = new dyCountry(config);

Step 3: Call methods like get to fetch data of a country based on ISO-Alpha-2 or ISO-Alpha-3 code.

var data = obj.get('IN');

Methods

get() returns the country detail

Pass ISO-Alpha-2 or ISO-Alpha-3 country code to the method and it will return the country information.

obj.get('IN');
obj.get('IND');

Result

{
  "name": "India",
  "capital": "New Delhi",
  "continent": {
    "code": "AS",
    "name": "Asia"
  },
  "iso": {
    "alpha2": "IN",
    "alpha3": "IND",
    "numeric": "356"
  },
  "timezone": {
    "capital": "Asia/Kolkata"
  },
  "latlong": {
    "lat": 20.593684,
    "long": 78.96288
  },
  "topLevelDomain": "in",
  "fipsCode": "IN",
  "phoneCode": [
    "91"
  ],
  "currencies": [
    "INR"
  ],
  "languages": [
    "en-IN"
  ],
  "flag": "dist/flags/in.png"
}

name() returns the country name

Pass ISO-Alpha-2 or ISO-Alpha-3 country code to the method and it will return the country name.

obj.name('IN');
obj.name('IND');

Result

"India"

capital() returns the country capital

Pass ISO-Alpha-2 or ISO-Alpha-3 country code to the method and it will return the country capital.

obj.capital('IN');
obj.capital('IND');

Result

"New Delhi"

flag() returns the country flag image path

Pass ISO-Alpha-2 or ISO-Alpha-3 country code to the method and it will return the country flag image path.

obj.flag('IN');
obj.flag('IND');

Result

"dist/flags/in.png"

Using the flag path in img HTML element:

India Flag

latlong() returns the country lat-long data

Pass ISO-Alpha-2 or ISO-Alpha-3 country code to the method and it will return the country lat-long data.

obj.latlong('IN');
obj.latlong('IND');

Result

{
  "lat": 20.593684,
  "long": 78.96288
}

Pass ISO-Alpha-2 or ISO-Alpha-3 country code and key to the method and it will return either country lat/long country data.

obj.latlong('IN', 'lat');
obj.latlong('IND', 'lat');

Result

20.593684

iso() returns the country iso data

Pass ISO-Alpha-2 or ISO-Alpha-3 country code to the method and it will return the country iso data.

obj.iso('IN');
obj.iso('IND');

Result

{
  "alpha2": "IN",
  "alpha3": "IND",
  "numeric": "356"
}

Pass ISO-Alpha-2 or ISO-Alpha-3 country code and key to the method and it will return specify country iso data.

obj.iso('IN', 'alpha3');
obj.iso('IND', 'alpha3');

Result

"IND"

continent() returns the country continent data

Pass ISO-Alpha-2 or ISO-Alpha-3 country code to the method and it will return the country continent data.

obj.continent('IN');
obj.continent('IND');

Result

{
  "code": "AS",
  "name": "Asia"
}

Pass ISO-Alpha-2 or ISO-Alpha-3 country code and key to the method and it will return specify country continent data.

obj.continent('IN', 'name');
obj.continent('IND', 'name');

Result

"Asia"

tld() returns the country top level domain

Pass ISO-Alpha-2 or ISO-Alpha-3 country code to the method and it will return the country tld.

obj.tld('IN');
obj.tld('IND');

Result

"in"

timezone() returns the country timezone for capital

Pass ISO-Alpha-2 or ISO-Alpha-3 country code to the method and it will return the country timezone.

obj.timezone('IN');
obj.timezone('IND');

Result

"Asia/Kolkata"

fipsCode() returns the country fips code

Pass ISO-Alpha-2 or ISO-Alpha-3 country code to the method and it will return the country fips code.

obj.fipsCode('IN');
obj.fipsCode('IND');

Result

"IN"

phoneCode() returns the country phone code

Pass ISO-Alpha-2 or ISO-Alpha-3 country code to the method and it will return the country phone code.

obj.phoneCode('IN');
obj.phoneCode('IND');

Result

[
  "91"
]

currencies() returns the country currencies

Pass ISO-Alpha-2 or ISO-Alpha-3 country code to the method and it will return the country currencies.

obj.currencies('IN');
obj.currencies('IND');

Result

[
  "INR"
]

languages() returns the country languages

Pass ISO-Alpha-2 or ISO-Alpha-3 country code to the method and it will return the country languages.

obj.languages('IN');
obj.languages('IND');

Result

[
  "en-IN"
]

all() returns all the countries detail

It will return all the countries detail.

obj.all();

Result

[
  { countryData },
  { countryData },
  ...
  ...
]

search() returns all the countries matching the search query

Pass string query to the method and it will return all the countries matching the country name.

obj.search('India');

Result

{
  "match": N,
  "result": [
    { countryData },
    { countryData }
    ...
    ...
  ]
}

Where, match is the total number of matching result. If no match is found then N will be 0 and result will be an empty array [].

Search by name

Pass object with name parameter query to the method and it will return all the countries matching country name.

obj.search({ name: 'India' });

Pass array of names and it will return all the matching countries.

obj.search({ name: ['ind', 'aus'] });

Search by capital

Pass object with capital parameter query to the method and it will return all the countries matching country capital.

obj.search({ capital: 'Delhi' });

Pass array of capitals and it will return all the matching countries.

obj.search({ capital: ['delhi', 'tokyo'] });

Search by topLevelDomain

Pass object with topLevelDomain parameter query to the method and it will return all the countries matching Top Level Domain.

obj.search({ topLevelDomain: 'in' });

Pass array of topLevelDomain and it will return all the matching countries.

obj.search({ topLevelDomain: ['in', 'au'] });

Search by fipsCode

Pass object with fipsCode parameter query to the method and it will return all the countries matching FIPS code.

obj.search({ fipsCode: 'in' });

Pass array of fipsCode and it will return all the matching countries.

obj.search({ fipsCode: ['in', 'us'] });

Search by phoneCode

Pass object with phoneCode parameter query to the method and it will return all the countries matching phone code.

obj.search({ phoneCode: '91' });

Pass an array of phone code and it will return all the matching countries.

obj.search({ phoneCode: ['90', '91'] });

Search by currencies

Pass object with currencies parameter query to the method and it will return all the countries matching currencies.

obj.search({ currencies: 'INR' });

Pass array of currencies and it will return all the matching countries.

obj.search({ currencies: ['INR', 'USD'] });

Search by languages

Pass object with languages parameter query to the method and it will return all the countries matching languages.

obj.search({ languages: 'hi' });

Pass array of languages and it will return all the matching countries.

obj.search({ languages: ['hi', 'la'] });

Search by continentCode

Pass object with continentCode parameter query to the method and it will return all the countries having that continent code.

obj.search({ continentCode: 'AS' });

Pass array of continent code and it will return all the matching countries.

obj.search({ continentCode: ['AS', 'AN'] });

Search by continentName

Pass object with continentName parameter query to the method and it will return all the countries having that continent name.

obj.search({ continentName: 'Asia' });

Pass array of continent names and it will return all the matching countries.

obj.search({ continentName: ['Asia', 'Antarctica'] });

Search by multi parameters

Pass object with multiple query parameters to the method and it will return all the matching countries.

Following example will fetch all contries having string in in its name AND string del in its capital.

obj.search({ name: 'in', capital: 'del' });

More methods...

  • getSelectHTML() returns HTML select element of countries

Check the index.html file of this project.

License

It's free and released under MIT License Copyright (c) 2018 Yusuf Shakeel

Do whatever you want with this :-)

Donate

Feeling generous :-) Buy me a cup of tea.

Donate via PayPal

install

npm i dycountryjs

Downloadsweekly downloads

100

version

0.8.1

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability