continent-country-map
v1.0.1
Published
[DEPRECATED] A clean, reliable dataset mapping continents to their respective countries using ISO 3166 alpha-2 codes.
Maintainers
Readme
⚠️ DEPRECATED
This package is no longer maintained.
Please use html-entities instead. See: https://www.npmjs.com/package/html-entities
Original README below:
continent-country-map
A modern, well-tested, and developer-friendly TypeScript library providing a clean and reliable dataset mapping continents to their respective countries using ISO 3166-1 alpha-2 codes. Perfect for dashboards, forms, filters, and geolocation-based logic.
Features
- 🗺️ Comprehensive continent-to-country mapping
- 🔒 Zero runtime dependencies
- 📦 Tree-shakable ESM and CommonJS support
- 🎯 TypeScript-first with full type safety
- ✅ Thoroughly tested with 100% coverage
- 🚀 Case-insensitive lookups
- 📚 Well-documented API
Installation
npm install continent-country-mapUsage
Basic Usage
import {
continentCountryMap,
getCountriesByContinent,
getContinentByCountryCode,
isValidContinent,
isCountryInContinent,
} from 'continent-country-map';
// Get all countries in Europe
const europeanCountries = getCountriesByContinent('Europe');
console.log(europeanCountries);
// ['AL', 'AD', 'AT', 'BY', 'BE', ...]
// Get the continent for a country code
const continent = getContinentByCountryCode('US');
console.log(continent);
// 'North America'
// Check if a string is a valid continent name
if (isValidContinent('Europe')) {
console.log('Valid continent!');
}
// Check if a country belongs to a continent
const isUSInEurope = isCountryInContinent('US', 'Europe');
console.log(isUSInEurope);
// falseReal-world Examples
Form Validation
import { getContinentByCountryCode } from 'continent-country-map';
function validateCountryContinent(countryCode: string, continent: string): boolean {
const actualContinent = getContinentByCountryCode(countryCode);
return actualContinent?.toLowerCase() === continent.toLowerCase();
}
// Usage in a form
const isValid = validateCountryContinent('US', 'North America');Filtering Countries by Continent
import { getCountriesByContinent } from 'continent-country-map';
function filterCountriesByContinent(countries: string[], continent: string): string[] {
const continentCountries = getCountriesByContinent(continent) || [];
return countries.filter((country) => continentCountries.includes(country.toUpperCase()));
}
// Usage
const countries = ['US', 'DE', 'FR', 'JP'];
const europeanCountries = filterCountriesByContinent(countries, 'Europe');
// ['DE', 'FR']Dashboard Continent Selection
import { isValidContinent, getCountriesByContinent } from 'continent-country-map';
function handleContinentSelection(continent: string) {
if (!isValidContinent(continent)) {
console.error('Invalid continent selected');
return;
}
const countries = getCountriesByContinent(continent);
// Update dashboard with country data...
}API Reference
Data
continentCountryMap
The main data structure mapping continents to their respective country codes.
const map = {
'Africa': ['DZ', 'AO', 'BJ', ...],
'Antarctica': ['AQ', 'BV', ...],
'Asia': ['AF', 'AM', 'AZ', ...],
// ...
};Functions
getCountriesByContinent(continentName: string): string[] | undefined
Returns an array of ISO 3166-1 alpha-2 country codes for a given continent.
- Parameters:
continentName: The name of the continent (case-insensitive)
- Returns: Array of country codes or
undefinedif continent not found
getContinentByCountryCode(countryCode: string): string | undefined
Returns the continent name for a given country code.
- Parameters:
countryCode: The ISO 3166-1 alpha-2 country code (case-insensitive)
- Returns: Continent name or
undefinedif country code not found
isValidContinent(continentName: string): boolean
Type guard that checks if a string is a valid continent name.
- Parameters:
continentName: The string to check
- Returns:
trueif the string is a valid continent name
isCountryInContinent(countryCode: string, continentName: string): boolean
Checks if a country belongs to a specific continent.
- Parameters:
countryCode: The ISO 3166-1 alpha-2 country codecontinentName: The name of the continent
- Returns:
trueif the country belongs to the specified continent
Types
type ContinentName =
| 'Africa'
| 'Antarctica'
| 'Asia'
| 'Europe'
| 'North America'
| 'Oceania'
| 'South America';
type CountryCodeA2 = string; // ISO 3166-1 alpha-2 code
type ContinentCountryMapData = Record<ContinentName, CountryCodeA2[]>;Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT
