npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@prince13/countriesdata

v0.1.1

Published

A package that provides all information about countries.

Downloads

2

Readme

Country Information Library

Table of Contents

  1. Introduction
  2. Installation
  3. Basic Usage
  4. Detailed API
  5. Advanced Examples
  6. Usage Tips
  7. Troubleshooting
  8. Contributing
  9. 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 names
  • tld: Top-level domains
  • cca2, cca3, ccn3: Country codes
  • currencies: Currencies used
  • capital: Capital(s)
  • region, subregion: Region and subregion
  • languages: Spoken languages
  • borders: Bordering countries
  • area: Area
  • population: Population
  • flags: 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

  1. Always reset filters with reset() after a search if you plan to make other queries.
  2. Use method chaining for complex queries.
  3. 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:

  1. Fork the repository
  2. Create your feature branch
  3. Commit your changes
  4. Push to the branch
  5. Open a Pull Request

License

This library is licensed under the MIT License. See the LICENSE file for more details.