@mohaned.ghawar/countries-info
Version:
A comprehensive JavaScript library for accessing detailed country information, including regions, capitals, continents, and more.
143 lines (127 loc) • 4.84 kB
JavaScript
const fs = require('fs');
const path = require('path');
class CountriesService {
constructor() {
const countriesPath = path.join(__dirname, '..', 'data', 'countries.json');
const regionsPath = path.join(__dirname, '..', 'data', 'regions.json');
const citiesPath = path.join(__dirname, '..', 'data', 'cities.json');
this.countries = JSON.parse(fs.readFileSync(countriesPath, 'utf8'));
this.regions = JSON.parse(fs.readFileSync(regionsPath, 'utf8'));
this.cities = JSON.parse(fs.readFileSync(citiesPath, 'utf8'));
}
/**
* Get all countries
* @returns {Array} Array of country objects
*/
getAllCountries() {
return Object.values(this.countries);
}
/**
* Get a country by its code
* @param {string} code - The country code (e.g., "USA", "GBR")
* @returns {Object|null} Country object or null if not found
*/
getCountryByCode(code) {
if (!code) return null;
return this.countries[code.toUpperCase()] || null;
}
/**
* Get all countries in a specific continent
* @param {string} continent - The continent name
* @returns {Array} Array of country objects in the specified continent
*/
getCountriesByContinent(continent) {
if (!continent) return [];
return Object.values(this.countries).filter(country =>
country.continent.toLowerCase() === continent.toLowerCase()
);
}
/**
* Search countries by name
* @param {string} name - The country name to search for
* @returns {Array} Array of matching country objects
*/
searchByName(name) {
if (!name) return [];
const searchTerm = name.toLowerCase();
return Object.values(this.countries).filter(country =>
country.name.common.toLowerCase().includes(searchTerm)
);
}
/**
* Get regions for a specific country
* @param {string} code - The country code (e.g., "USA", "GBR")
* @returns {Array} Array of region names for the specified country
*/
getCountryRegions(code) {
if (!code) return [];
return this.regions[code.toUpperCase()] || [];
}
/**
* Get a country by its name
* @param {string} name - The country name
* @returns {Object|null} Country object or null if not found
*/
getCountryByName(name) {
if (!name) return null;
const searchTerm = name.toLowerCase();
return Object.values(this.countries).find(country =>
country.name.common.toLowerCase() === searchTerm
) || null;
}
/**
* Get all cities in a specific region of a country
* @param {string} countryCode - The country code (e.g., "USA", "GBR")
* @param {string} regionName - The name of the region/state/province
* @returns {Array} Array of city objects in the specified region
*/
getCitiesByRegion(countryCode, regionName) {
if (!countryCode || !regionName) return [];
const countryData = this.cities[countryCode.toUpperCase()];
if (!countryData) return [];
return countryData[regionName] || [];
}
/**
* Get all cities in a country
* @param {string} countryCode - The country code (e.g., "USA", "GBR")
* @returns {Array} Array of all city objects in the country
*/
getAllCitiesInCountry(countryCode) {
if (!countryCode) return [];
const countryData = this.cities[countryCode.toUpperCase()];
if (!countryData) return [];
return Object.values(countryData).flat();
}
/**
* Get the total number of cities in a country
* @param {string} countryCode - The country code (e.g., "USA", "GBR")
* @returns {number} Total number of cities in the country
*/
getCityCount(countryCode) {
return this.getAllCitiesInCountry(countryCode).length;
}
/**
* Search for cities across all countries
* @param {string} name - The city name to search for
* @returns {Array} Array of matching city objects with country and region info
*/
searchCities(name) {
if (!name) return [];
const searchTerm = name.toLowerCase();
const results = [];
for (const [countryCode, countryData] of Object.entries(this.cities)) {
for (const [regionName, cities] of Object.entries(countryData)) {
const matchingCities = cities.filter(city =>
city.name.toLowerCase().includes(searchTerm)
).map(city => ({
...city,
countryCode,
regionName
}));
results.push(...matchingCities);
}
}
return results;
}
}
module.exports = CountriesService;