@prince13/countriesdata
v0.1.1
Published
A package that provides all information about countries.
Downloads
2
Maintainers
Readme
Country Information Library
Table of Contents
- Introduction
- Installation
- Basic Usage
- Detailed API
- Advanced Examples
- Usage Tips
- Troubleshooting
- Contributing
- License
Introduction
This JavaScript library provides easy and flexible access to detailed information about countries worldwide. It allows developers to search, filter, and retrieve data about countries, such as flags, capitals, populations, and much more.
Installation
To install the library, use npm:
npm install countriesData
Basic Usage
Here's how to get started with the library:
import { CountriesData } from 'countriesData';
// Create an instance of CountryManager
const countriesData = CountriesData();
// Example: Get names of all European countries
const europeanCountries = countriesData.continent('europe').names;
console.log(europeanCountries);
Detailed API
CountryManager Class
Filtering Methods
continent(continent: string): CountryManager
- Filters countries by continent.
- Example:
countriesData.continent('africa')
byPopulation(min: number, max?: number): CountryManager
- Filters countries by population.
- Example:
countriesData.byPopulation(1000000, 5000000)
byArea(min: number, max?: number): CountryManager
- Filters countries by area.
- Example:
countriesData.byArea(100000)
byName(name: string): CountryManager
- Searches countries by name (common or official).
- Example:
countriesData.byName('france')
Data Retrieval Methods
flags: { country: string, flag: string | undefined }[]
- Returns an array of objects containing the country name and its flag URL.
names: string[]
- Returns an array of common country names.
capitals: { country: string, capital: string | undefined }[]
- Returns an array of objects containing the country name and its capital.
getResults(limit?: number): Country[]
- Returns complete results of filtered countries, with an optional limit.
getCountryByCode(code: string): Country | undefined
- Returns information about a specific country by its code (cca2, cca3, or ccn3).
Other Methods
reset(): CountryManager
- Resets filters, returning the country list to its initial state.
Country Interface
The Country
interface represents the data structure for each country. It includes properties such as:
name
: Object containing common and official country namestld
: Top-level domainscca2
,cca3
,ccn3
: Country codescurrencies
: Currencies usedcapital
: Capital(s)region
,subregion
: Region and subregionlanguages
: Spoken languagesborders
: Bordering countriesarea
: Areapopulation
: Populationflags
: Flag URLs- ... and many more
Advanced Examples
Find Asian countries with a population over 100 million
const largeAsianCountries = countriesData
.continent('asia')
.byPopulation(100000000)
.names;
console.log(largeAsianCountries);
Get flags of the 5 largest South American countries
const southAmericanFlags = countriesData
.continent('south america')
.sortByArea(false)
.getResults(5)
.map(country => ({
name: country.name.common,
flag: country.flags.svg
}));
console.log(southAmericanFlags);
Usage Tips
- Always reset filters with
reset()
after a search if you plan to make other queries. - Use method chaining for complex queries.
- For optimal performance, create a single instance of
CountryManager
and reuse it.
Troubleshooting
Problem: No results returned Solution: Check that you haven't applied overly restrictive filters. Use
reset()
to start over.Problem: Error "Cannot read property of undefined" Solution: Ensure that the property you're trying to access exists for all countries. Use null checks or default values.
Contributing
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Open a Pull Request
License
This library is licensed under the MIT License. See the LICENSE file for more details.