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

full-countries-states-cities

v1.0.2

Published

Library for fetching all Countries States and Cities with typescript support and pagination

Downloads

175

Readme

🌍 full-countries-states-cities package

Welcome to the Location Data Package! This package provides well-formatted, all-English data on countries, states, and cities, including projection and pagination support for efficient data retrieval. It can be easily integrated into any project and includes a wide range of features for working with geographical data.

📦 What's Included

This package includes the following features:

  1. Country Data: Retrieve information on all countries, with optional projection for specific fields.
  2. State Data: Get details on states within a specific country, with support for pagination and field projection.
  3. City Data: Fetch cities based on country ISO codes, with the ability to filter by state and use pagination.
  4. Field Projection: Specify which fields you want returned (e.g., only name and iso).
  5. Pagination: Easily control how many items are returned and navigate through large datasets.
  6. All the names that should be in english are cleared from unknown symbols!

📚 How to Use

Installation

First, install the package using npm:

npm install full-countries-states-cities

Importing Functions

You can import the available functions like so:

import {
    getCountries,
    getCountry,
    getStates,
    getCities
} from 'full-countries-states-cities'

Easy as that

getCountries() function will give u back:

{
    "id": 1,
    "name": "Afghanistan",
    "iso3": "AFG",
    "iso2": "AF",
    "numeric_code": "004",
    "phone_code": "93",
    "capital": "Kabul",
    "currency": "AFN",
    "currency_name": "Afghan afghani",
    "currency_symbol": "؋",
    "tld": ".af",
    "native": "افغانستان",
    "region": "Asia",
    "region_id": "3",
    "subregion": "Southern Asia",
    "subregion_id": "14",
    "nationality": "Afghan",
    "timezones": [
        {
            "zoneName": "Asia/Kabul",
            "gmtOffset": 16200,
            "gmtOffsetName": "UTC+04:30",
            "abbreviation": "AFT",
            "tzName": "Afghanistan Time"
        }
    ],
    "translations": {
        "kr": "아프가니스탄",
        "pt-BR": "Afeganistão",
        "pt": "Afeganistão",
        "nl": "Afghanistan",
        "hr": "Afganistan",
        "fa": "افغانستان",
        "de": "Afghanistan",
        "es": "Afganistán",
        "fr": "Afghanistan",
        "ja": "アフガニスタン",
        "it": "Afghanistan",
        "cn": "阿富汗",
        "tr": "Afganistan"
    },
    "latitude": "33.00000000",
    "longitude": "65.00000000",
    "emoji": "🇦🇫",
    "emojiU": "U+1F1E6 U+1F1EB",
    "iso": "AF"
}

Available Functions

1. getCountries

Retrieve a list of all countries with support for pagination and field projection.

const result = getCountries({ page: 1, limit: 10, project: ['name', 'iso'] });
console.log(result.countries); // Array of countries with 'name' and 'iso' fields

Parameters:

  • page: Page number (default: 1)
  • limit: Number of countries per page (set to 0 to retrieve all)
  • project: Array of fields to return (optional)

2. getCountry

Retrieve full details of a specific country by its ISO code.

const country = getCountry('US');
console.log(country); // Returns the details for the country with ISO code 'US'

Parameters:

  • isoCode: ISO 3166-1 alpha-2 code of the country.

3. getStates

Retrieve a list of states within a country, with support for pagination and field projection.

const result = await getStates({
    isoCode: 'US',
    limit: 5,
    page: 1,
    project: ['name', 'abbreviation']
});
console.log(result.states); // Array of states with 'name' and 'abbreviation' fields

Parameters:

  • isoCode: ISO 3166-1 alpha-2 code of the country.
  • page: Page number (default: 1)
  • limit: Number of states per page (set to 0 to retrieve all)
  • project: Array of fields to return (optional)

4. getCities

Retrieve a list of cities within a country, with optional filtering by state, pagination, and field projection.

const result = await getCities({
    isoCode: 'US',
    state: 'California',
    limit: 10,
    project: ['name', 'population']
});
console.log(result.cities); // Array of cities in California with 'name' and 'population' fields

Parameters:

  • isoCode: ISO 3166-1 alpha-2 code of the country.
  • state: Optional state name to filter cities by.
  • page: Page number (default: 1)
  • limit: Number of cities per page (set to 0 to retrieve all)
  • project: Array of fields to return (optional)

🔄 Pagination Support

For endpoints that support pagination, you can control the number of results and navigate through pages.

  • page: The current page number (starting from 1).
  • limit: Number of results per page.

Example: Fetch the second page of states for the United States, with a limit of 5 states per page:

const result = await getStates({ isoCode: 'US', page: 2, limit: 5 });
console.log(result.pagination); // Pagination info including current page, total pages, etc.

Example Pagination Response:

{
    "states": [],
    "pagination": {
        "totalStates": 50,
        "totalPages": 10,
        "currentPage": 2,
        "hasMore": true
    }
}

🛠️ Example Usage

Fetching Countries with Field Projection

const countries = getCountries({ limit: 5, project: ['name', 'iso'] });
console.log(countries);

Fetching Cities by Country and State

const cities = await getCities({
    isoCode: 'US',
    state: 'California',
    limit: 10
});
console.log(cities);

Fetching States with Pagination

const states = await getStates({ isoCode: 'US', page: 2, limit: 5 });
console.log(states);

📝 Notes

  • The project parameter can be used to limit the fields returned, reducing payload size and improving performance.
  • Pagination parameters allow you to control the number of results returned and navigate through large datasets.