Skip to content

Latest commit

Β 

History

10 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

@greycode/country_utils

npm version install size license

A high-performance, lightweight utility for working with country-related data. Optimized for both Node.js and Browser environments with full TypeScript support.

πŸš€ Features

  • Blazing Fast: O(1) lookups using optimized Maps.
  • Comprehensive Data: Includes ~250 countries with ISO codes (Alpha-2, Alpha-3), numeric codes, phone codes, currencies, capitals, continents, and flag emojis.
  • Maintainable Architecture: Data is modularized by continent for easy updates and smaller bundles.
  • Modern: Built with ESM and CommonJS support (dual-build).
  • Type-Safe: Comprehensive TypeScript definitions included.
  • Zero Dependencies: Pure, lightweight logic.

πŸ“¦ Installation

npm install @greycode/country_utils

πŸ›  Usage

Modern ESM / TypeScript

import { getByCountryCode, getAllCountries, getByPhoneCode } from '@greycode/country_utils';

// Get a country by ISO Alpha-2 code
const code = getByCountryCode('ZW');

if (code.isValid) {
  console.log(code.country);   // Zimbabwe
  console.log(code.phoneCode); // +263
  console.log(code.flag);      // πŸ‡ΏπŸ‡Ό
}

CommonJS

const { CountryUtil } = require('@greycode/country_utils');

const uk = CountryUtil.getByCountryCode('GB');
console.log(uk.capital); // London

πŸ“– API Reference

getByCountryCode(code: string): LookupResult

Lookup a country by its ISO 3166-1 alpha-2 code. Returns a LookupResult.

getByAlpha3(code: string): LookupResult

Lookup a country by its ISO 3166-1 alpha-3 code. Returns a LookupResult.

getByPhoneCode(code: string): LookupResult

Lookup a country by its phone dialing code (e.g., +1 or +263).

getCountryCount(): number

Returns the total number of countries in the database.

getContinents(): string[]

Returns a unique, sorted list of all continents.

getCountryCountByContinent(continent: string): number

Returns the count of countries for a given continent.

getAllCountries(): Country[]

Returns the full array of all 250+ country objects.

getByContinent(continent: string): Country[]

Returns all countries belonging to a specific continent (e.g., "Africa", "Europe").

πŸ’‘ Use Cases & Examples

🌐 Vanilla HTML/JS (via CDN/Bundler)

If you're using a bundler (Vite, Webpack) or a modern browser with ESM support:

<script type="module">
  import { getAllCountries } from 'https://cdn.jsdelivr.net/npm/@greycode/country_utils/+esm';
  
  const list = getAllCountries();
  console.log(`Loaded ${list.length} countries!`);
</script>

βš›οΈ React

A simple country selector component:

import React, { useState } from 'react';
import { getAllCountries, Country } from '@greycode/country_utils';

export const CountrySelector = () => {
  const [selected, setSelected] = useState<string>('');
  const countries = getAllCountries();

  return (
    <select value={selected} onChange={(e) => setSelected(e.target.value)}>
      <option value="">Select a country</option>
      {countries.map((c: Country) => (
        <option key={c.countryCode} value={c.countryCode}>
          {c.flag} {c.country}
        </option>
      ))}
    </select>
  );
};

πŸ–– Vue 3

Filtering countries by continent in a composition API component:

<script setup>
import { getByContinent } from '@greycode/country_utils';
import { computed } from 'vue';

const africanCountries = computed(() => getByContinent('Africa'));
</script>

<template>
  <ul>
    <li v-for="c in africanCountries" :key="c.countryCode">
      {{ c.flag }} {{ c.country }} ({{ c.phoneCode }})
    </li>
  </ul>
</template>

⏭️ Next.js (App Router)

Using analytic functions in a Server Component:

import { getCountryCount, getContinents } from '@greycode/country_utils';

export default function Dashboard() {
  const total = getCountryCount();
  const continents = getContinents();

  return (
    <div>
      <h1>Global Stats</h1>
      <p>Tracking {total} countries across {continents.length} continents.</p>
    </div>
  );
}

πŸ“‚ Data Structure

The package now uses a modular data structure located in src/data/. Each continent has its own JSON file:

  • africa.json
  • asia.json
  • europe.json
  • north_america.json
  • south_america.json
  • oceania.json
  • antarctic.json

This makes it easier to keep the data updated and allows for better maintenance.

interface Country {
  country: string;
  countryCode: string; // ISO Alpha-2
  alpha3: string;      // ISO Alpha-3
  numeric: string;
  phoneCode: string;
  currency: string;
  capital: string;
  continent: string;
  flag: string;        // Emoji flag
}

πŸ§ͺ Testing

This package is fully tested with Vitest.

npm test

πŸ“„ License

MIT Β© Kudzai Munyama

About

A utility class for working with country data, including country codes, phone country codes, and more.

Topics

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages