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 🙏

© 2026 – Pkg Stats / Ryan Hefner

country-data-finder

v1.0.10

Published

Get country name, flag URL, postal code format, and states/divisions by country code or country name

Downloads

889

Readme

Country Data Finder

A comprehensive, lightweight library to get country name, country code, flag URL, postal code format, and states/divisions by providing a country code or country name.

npm version License: MIT npm downloads

GitHub Repository: https://github.com/masudrana2779/country-data-finder


Features

  • Comprehensive Data — 249 countries with states/divisions, postal code formats, and flag URLs
  • TypeScript Support — Full built-in type definitions
  • React HookuseCountryInfo() with memoization for React 16.8+
  • Universal — Works with Node.js, React, Next.js, or any JavaScript/TypeScript project
  • Zero Dependencies — Lightweight and self-contained (React is optional peer dependency)
  • Flexible Search — Look up by ISO 3166-1 alpha-2 code or country name (case-insensitive)
  • Postal Code Patterns — Regex-based postal/zip code formats for validation

Installation

npm install country-data-finder
yarn add country-data-finder
pnpm add country-data-finder

Quick Start

import { getCountryInfo } from "country-data-finder";

// Search by country code
const info = getCountryInfo("US");
console.log(info);
// {
//   countryName: "United States",
//   countryCode: "US",
//   flagUrl: "https://flagcdn.com/w80/us.png",
//   states: ["Alabama", "Alaska", "Arizona", ... "Wyoming"]
// }

// Search by country name (case-insensitive)
const info2 = getCountryInfo("united states");
console.log(info2);
// {
//   countryName: "United States",
//   countryCode: "US",
//   flagUrl: "https://flagcdn.com/w80/us.png",
//   states: ["Alabama", "Alaska", "Arizona", ... "Wyoming"]
// }

API Reference

getCountryInfo(input: string): CountryInfo

Returns full country details for a given country code or name.

import { getCountryInfo } from "country-data-finder";

const result = getCountryInfo("US");
// result.countryName  → "United States"
// result.countryCode  → "US"
// result.flagUrl      → "https://flagcdn.com/w80/us.png"
// result.states       → ["Alabama", "Alaska", "Arizona", ... "Wyoming"]

If the input does not match any country:

const result = getCountryInfo("XYZ");
// { countryName: "XYZ", countryCode: "", flagUrl: null, states: [] }

getStates(input: string): string[]

Returns only the states/divisions of a country.

import { getStates } from "country-data-finder";

const states = getStates("US");
// ["Alabama", "Alaska", "Arizona", ... "Wyoming"]

const states2 = getStates("United Kingdom");
// ["England", "Northern Ireland", "Scotland", "Wales"]

getAllCountries(): { name: string; code: string }[]

Returns a list of all 249 countries with name and code.

import { getAllCountries } from "country-data-finder";

const countries = getAllCountries();
// [
//   { name: "Afghanistan", code: "AF" },
//   { name: "Albania", code: "AL" },
//   ...
// ]

useCountryInfo(input: string): CountryInfo — React Hook

A React hook that returns memoized country info. Requires React 16.8+.

import { useCountryInfo } from "country-data-finder";

function CountryCard({ code }: { code: string }) {
  const { countryName, countryCode, flagUrl, states } = useCountryInfo(code);

  return (
    <div>
      {flagUrl && <img src={flagUrl} alt={countryName} />}
      <h2>
        {countryName} ({countryCode})
      </h2>
      <p>{states.length} states/divisions</p>
      <ul>
        {states.map((state) => (
          <li key={state}>{state}</li>
        ))}
      </ul>
    </div>
  );
}

Available Data

Each country entry includes:

| Field | Type | Description | | ---------- | ---------- | --------------------------------------------------------------------------- | | name | string | English country name | | code | string | ISO 3166-1 alpha-2 code | | postCode | string | Postal/zip code regex pattern (empty if none) | | states | string[] | List of states, provinces, or divisions |


TypeScript Types

The package exports the following types:

import type { CountryInfo, Country } from "country-data-finder";

// CountryInfo — returned by getCountryInfo() and useCountryInfo()
interface CountryInfo {
  countryName: string;
  countryCode: string;
  flagUrl: string | null;
  states: string[];
}

// Country — shape of each country in the data
interface Country {
  name: string;
  code: string;
  postCode: string;
  states: string[];
}

Supported Input Formats

| Input | Example | Description | | ---------------- | ----------------- | ----------------------- | | Country code | "US", "GB" | ISO 3166-1 alpha-2 code | | Country name | "United States" | Full country name | | Case-insensitive | "united states" | Works with any casing |


Contribute

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License — see the LICENSE file for details.