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-js

v0.12.1

Published

Get the country code, name, currency symbol, capital, phone code, latitude and longitude

Readme

country-js

country-js is an npm package designed to provide extensive information about countries, encompassing details such as country code, name, currency symbol, capital city, phone code, latitude, and longitude.

Installation

You can easily install country-js via npm:

npm install country-js --save

Features

  • ✅ Search countries by code, name, currency, capital, or phone
  • ✅ Direct country lookup by code or name
  • ✅ Get all countries or count total
  • ✅ Optimized search with exact match prioritization
  • ✅ No duplicate results
  • ✅ Input validation & error handling
  • ✅ TypeScript support with type definitions
  • ✅ Comprehensive test coverage

Usage

Basic Import

const countryInfo = require('country-js');

1. Search Function

Search by country code, name, currency code, currency name, currency symbol, capital, or phone code.

// Search by country code
countryInfo.search('US');        // Returns USA
countryInfo.search('at');        // Returns Austria (case-insensitive)

// Search by country name
countryInfo.search('austria');   // Returns Austria
countryInfo.search('FRANCE');    // Returns France

// Search by currency
countryInfo.search('euro');      // Returns all Euro countries
countryInfo.search('eur');       // Returns all EUR countries
countryInfo.search('€');         // Returns all Euro countries

// Search by capital
countryInfo.search('PARIS');     // Returns France

// Search by phone code
countryInfo.search('1');         // Returns USA, Canada, etc.

Returns: Array of country objects (prioritizes exact matches first)

// Example output:
/*
[
  {
    code: 'AT',
    name: 'AUSTRIA',
    currency: {
      currencyName: 'EURO',
      currencyCode: 'EUR',
      currencySymbol: '€'
    },
    geo: { latitude: 47.516231, longitude: 14.550072 },
    capital: 'VIENNA',
    phone: '43'
  }
]
*/

2. Get By Code

Directly retrieve a country by its ISO code (faster than search).

const country = countryInfo.getByCode('US');
const country = countryInfo.getByCode('sg');  // case-insensitive

// Returns: Country object or null if not found

3. Get By Name

Directly retrieve a country by its name.

const country = countryInfo.getByName('AUSTRIA');
const country = countryInfo.getByName('france');  // case-insensitive

// Returns: Country object or null if not found

4. Get All Countries

Retrieve all available countries.

const allCountries = countryInfo.getAll();

// Returns: Array of all country objects
// Example: Array with 250+ countries

5. Count

Get the total number of countries in the database.

const total = countryInfo.count();

// Returns: 250 (or similar number)

Error Handling

The library validates input and throws TypeError for invalid parameters:

try {
    countryInfo.search(123);  // Throws TypeError
} catch (error) {
    console.error(error.message);
}

Valid inputs: search(), getByCode(), getByName() accept strings only Invalid inputs: null, undefined, numbers, objects, arrays (except empty strings return empty array)

Response Structure

Each country object contains:

{
  code: string;                    // ISO 3166-1 alpha-2 code (e.g., 'US')
  name: string;                    // Country name in uppercase
  currency: {
    currencyName: string;          // Currency name
    currencyCode: string;          // ISO 4217 code
    currencySymbol: string;        // Currency symbol
  };
  geo: {
    latitude: number;              // Geographic latitude
    longitude: number;             // Geographic longitude
  };
  capital: string;                 // Capital city name
  phone: string;                   // International phone code
}

TypeScript Support

Type definitions are included with the package:

import * as country from 'country-js';

const result: country.Country[] = country.search('US');
const usa: country.Country | null = country.getByCode('US');
const count: number = country.count();

Examples

const countryInfo = require('country-js');

// Get all Euro countries
const euroCountries = countryInfo.search('EUR');

// Get information about a specific country
const usa = countryInfo.getByCode('US');
console.log(usa.capital);  // Output: 'WASHINGTON'

// Find countries by capital
const parisCountry = countryInfo.search('PARIS');

// Check total countries
console.log('Total countries:', countryInfo.count());

// Get all countries
const all = countryInfo.getAll();
console.log(`Database contains ${all.length} countries`);

Performance Notes

  • search(): O(n) complexity, optimized with Set for deduplication
  • getByCode(): O(n) complexity (faster for direct lookups)
  • getByName(): O(n) complexity (faster for direct lookups)
  • getAll(): O(1) - instant

For best performance with frequent lookups by code or name, use getByCode() or getByName() instead of search().

Testing

Run the comprehensive test suite:

npm test

Tests cover:

  • Search functionality (by code, name, currency, capital, phone)
  • Direct getters (getByCode, getByName)
  • Edge cases (null, undefined, empty strings)
  • Error handling (invalid input types)
  • Data integrity (unique codes, valid coordinates)