countries-data-kit
is a powerful, minimal-sized JavaScript package offering over 15 types of detailed country information with advanced filtering options. It adhers to ISO 3166 for countries, ISO 4217 for currencies, and ISO 639 for languages.
-
π§ Wide Compatibility: Runs seamlessly in Node.js, React, React Native, and browser environments.
-
π Comprehensive Data: Fetch Currency Details, Get Dialing Codes, Display Flag Emojis, Provide Language Info, Access Emergency Numbers, Driving Side Info, Get Phone Number Masks
-
π ISO Standards Compliance: Adheres to ISO 3166 for countries, ISO 4217 for currencies, and ISO 639 for languages.
-
π Flexible Filtering: Filter data by over 10 different criteria to get precisely the information you need.
You can install the package via npm or yarn:
npm install countries-data-kit
or
yarn add countries-data-kit
The main function, getCountries({ fields:[], filters:{}|[] })
, retrieves country details based on two parameters:
-
fields: Specify which country data you want, such as mobile number mask, capital, or dialing code. Use the
CFields
enum or pass a string array. -
filters: Define criteria to filter countries, like continent, alpha-2/alpha-3 code, name, or dialing number.
This setup gives you control over the data you receive and how countries are selected.
const { getCountries, SFields, CFields } = require('countries-data-kit');
import { getCountries, CFields, SFields } from 'countries-data-kit';
To get data for all 249 countries, simply call the getCountries
function:
const countries = getCountries();
or
const countries = getCountries({});
If you only need certain fields, such as phone number details, you can specify them using the fields
parameter:
const countriesPhoneNumberDetails = getCountries({
fields: [
CFields.dialingCode,
CFields.phoneNumberMask
]
});
Alternatively, you can pass them as strings:
const countriesPhoneNumberDetails = getCountries({
fields: [
"dialingCode",
"phoneNumberMask"
]
});
To filter countries by certain criteria, such as continent or country code, use the filters
parameter.
Get all Asian and European countries:
const asianCountries = getCountries({
filters: [
SFields.continentNames.Asia
]
});
or
const asianCountries = getCountries({
filters: {
continentName:"Asia"
}
});
or
const asianCountries = getCountries({
filters: {
continentName:["Asia"]
}
});
You can also filter by specific country codes:
let countries = getCountries({
filters: {
alpha2Code: ["US", "CA", "EG", "JP"]
}
});
You can combine fields
and filters
to retrieve specific data. For example, to get the capital city of Germany:
let countries = getCountries({
fields: [CFields.capitalCity],
filters: [SFields.countryNames.Germany]
});
or
let countries = getCountries({
fields: [
CFields.capitalCity
],
filters: {
countryName: "Germany"
}
});
Retrieve in which continents specific countries exist based on their alpha3 codes:
let countries = getCountries({
fields: [
CFields.continentName
],
filters: {
alpha3Code: ["DZA", "AUT"]
}
});
You can apply multiple filters to retrieve data that meets several criteria. For example, to get all countries with Cairo as the capital, a specific flag, and those that use the Euro:
let countries = getCountries({
fields: [CFields.continentName],
filters: [
SFields.capitalCities.Cairo,
SFields.flagEmojis["πΊπΈ"],
SFields.currencyCodes.EUR
]
});
or
let countries = getCountries({
fields:[CFields.continentName],
filters:{
capitalCity:"Cairo",
flagEmoji:"πΊπΈ",
currencyCode:"EUR"
}
})
the above call will return all countries that use Euro + Egypt + USA
The package provides the following data fields: CFields
Field | Description | Example | How to Access |
---|---|---|---|
alpha2Code |
ISO 3166-1 alpha-2 code | "US" | CFields.alpha2Code |
alpha3Code |
ISO 3166-1 alpha-3 code | "USA" | CFields.alpha3Code |
capitalCity |
Capital city | "Washington, D.C." | CFields.capitalCity |
continentName |
Continent name | "North America" | CFields.continentName |
countryName |
Official country name | "United States of America" | CFields.countryName |
currency |
Official currency name | "United States dollar" | CFields.currency |
currencyCode |
ISO 4217 currency code | "USD" | CFields.currencyCode |
currencySymbol |
Currency symbol | "$" | CFields.currencySymbol |
dialingCode |
International dialing code | "+1" | CFields.dialingCode |
drivingSide |
Driving side ("left" or "right") | "right" | CFields.drivingSide |
emergencyNumbers |
List of emergency numbers | {"police": "911", "fire": "911", "ambulance": "911"} | CFields.emergencyNumbers |
flagEmoji |
Flag emoji | "πΊπΈ" | CFields.flagEmoji |
flagSVGUrl |
URL to SVG image of the flag | "https://example.com/flag.svg" | CFields.flagSVGUrl |
numericCountryCode |
Numeric country code | "840" | CFields.numericCountryCode |
phoneNumberMask |
Phone number format mask | "+1 (###) ###-####" | CFields.phoneNumberMask |
primaryLanguage |
Primary language | "English" | CFields.primaryLanguage |
This method allows you to filter countries by providing an array of values from the SFields
mappings.
-
Example:
const filteredCountries = getCountries({ filters: [SFields.alpha2Codes.US, SFields.continentNames.Africa] });
-
SFields Table:
Field How to Access alpha2Codes
SFields.alpha2Codes
alpha3Codes
SFields.alpha3Codes
capitalCities
SFields.capitalCities
continentNames
SFields.continentNames
countryNames
SFields.countryNames
currencies
SFields.currencies
currencyCodes
SFields.currencyCodes
dialingCodes
SFields.dialingCodes
drivingSides
SFields.drivingSides
flagEmojis
SFields.flagEmojis
numericCountryCodes
SFields.numericCountryCodes
phoneNumberMasks
SFields.phoneNumberMasks
primaryLanguages
SFields.primaryLanguages
This method provides more control by allowing you to specify filters as key-value pairs, where the keys correspond to the filter object keys and the values are the criteria you want to filter by.
-
Example:
const filteredCountries = getCountries({ filters: { continentName: ["Europe", "Asia"], alpha2Code: "EG" } });
-
Filter Object Key Table:
Filter Object Key Example alpha2Code
"EG"
alpha3Code
"USA"
capitalCity
"Cairo"
continentName
["Europe", "Asia"]
countryName
"United States of America"
currency
"Egyptian pound"
currencyCode
"USD"
dialingCode
["+20", "+1"]
drivingSide
"right"
flagEmoji
"πͺπ¬"
numericCountryCode
"840"
phoneNumberMask
"(###) ###-####"
primaryLanguage
["English", "Arabic"]
Contributions are welcome! Please check the contribution guidelines for more details.
This project is licensed under the MIT License. See the LICENSE file for details.
For questions or support, please open an issue on GitHub or contact us via email at [email protected].
Happy coding! π