dyCountryJS
Project

Dictionary to get country info.
Become a Patron
If you find this project useful then feel free to back it. I'm on Patreon
Feeling generous
Buy me a cup of tea Donate via PayPal
Contents
License
It's free and released under MIT License.
Getting started
You can start using this plugin in your project in any of the following ways.
- Download the latest release from dyCountryJS GitHub repository.
- Run the following NPM command
$ npm i dycountryjs
- Or use dyCountryJS via jsDelivr CDN.
Starter template
In the following example we have a starter template to get started.
<!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>
The dist
directory
You will find the JavaScript and images files inside the dist
directory.
JavaScript files are inside the js
directory.
Images of the flags are inside the flags
directory.
How to use?
Instantiate an object of the dyCountry
class.
var obj = new dyCountry();
Now you can use the obj
.
Config
The constructor takes the following configurations.
var config = {
flagDir: 'path/to/dist/flags'
};
var obj = new dyCountry(config);
Item | Purpose |
---|---|
flagDir |
(Optional) This is the complete path of the flag directory without trailing slash.
Example: https://example.com/path/to/dist/flags |
List of Methods
get()
returns the country detailname()
returns the country namecapital()
returns the country capitalflag()
returns the country flag image pathlatlong()
returns the country lat-long dataiso()
returns the country iso datacontinent()
returns the country continent datatld()
returns the country top level domaintimezone()
returns the country timezone for capitalfipsCode()
returns the country fips codephoneCode()
returns the country phone codecurrencies()
returns the country currencieslanguages()
returns the country languagesall()
returns all the countries detailsearch()
returns all the countries matching the search querygetSelectHTML()
returns HTML select element of countries
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');
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');
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');
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');
Using the flag path in img
HTML element:
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');
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');
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');
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');
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');
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');
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');
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');
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');
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');
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');
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');
all()
returns all the countries detail
It will return all the countries detail.
obj.all();
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');
Where match
is the total number of match.
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 query
Pass object with multiple parameter query 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 name.
obj.search({ name: 'in', capital: 'del' });
getSelectHTML()
returns HTML select element of countries
This will return a HTML select element as a string that can be used in the web page.
Pass the following optional configuration
Property | Type | Purpose |
---|---|---|
id |
string | (Optional) This will be the value for the id attribute. |
name |
string | (Optional) This will be the value for the name attribute. |
required |
boolean | (Optional) If true, then the required attribute will be added to the select element. |
size |
integer |
(Optional) This will control the total number of visible options.
Value like: 1, 2, 3... |
firstOptionElem |
object |
(Optional) This represents the first option HTML element
firstOptionElem.value is the value of the first option element.
firstOptionElem.text is the text of the first option element.
|
optionValue |
string |
(Optional) This will be the value for the value attribute for the option HTML
element.
If not set, then ISO-Alpha-2 value will be used. Allowed values:
|
countries |
object |
(Optional) This holds the ISO-Alpha-2 or ISO-Alpha-3 country code of all the countries to be
included in the list.
countries.list array of country code
Example: list: ['IN', 'JP']
countries.sort (optional) sort the country name in ascending 'ASC' or
descending 'DESC' order
Example: sort: 'ASC'
|
selectDataAttr |
array |
(Optional) Array of data attributes for the select HTML element.
Each object contains name-value pair. Example: { name: 'data-hello', value: 'world' }
|
var config = {
id: 'my-country-list',
name: 'my-country-list',
required: true,
firstOptionElem: {
value: '-',
text: '--- Select ---'
},
optionValue: 'alpha2',
countries: {
list: ['IN', 'JP', 'AU', 'CN', 'US', 'AE', 'GB', 'CA'],
sort: 'ASC'
},
selectDataAttr: [
{ name: 'data-hello', value: 'world }
]
};
obj.getSelectHTML(config);
Output