UNPKG

countries-region

Version:

A comprehensive library for countries and regions data with TypeScript support

110 lines (109 loc) 3.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAirports = exports.regions = exports.countries = void 0; exports.getCountries = getCountries; exports.getRegions = getRegions; exports.countriesOfRegion = countriesOfRegion; exports.countriesOfSubRegion = countriesOfSubRegion; exports.countryByISO2 = countryByISO2; exports.countryByISO3 = countryByISO3; exports.countryBySlug = countryBySlug; exports.countryByName = countryByName; exports.regionBySlug = regionBySlug; exports.regionsByName = regionsByName; const countries_1 = require("./data/countries"); Object.defineProperty(exports, "countries", { enumerable: true, get: function () { return countries_1.countries; } }); const regions_1 = require("./data/regions"); Object.defineProperty(exports, "regions", { enumerable: true, get: function () { return regions_1.regions; } }); const airports_1 = require("./modules/airports"); Object.defineProperty(exports, "getAirports", { enumerable: true, get: function () { return airports_1.getAirports; } }); /** * Get all countries * @returns Array of all countries */ function getCountries() { return [...countries_1.countries]; } /** * Get all regions * @returns Array of all regions */ function getRegions() { return [...regions_1.regions]; } /** * Get countries by region * @param regionSlug - The slug of the region to filter by * @returns Array of countries in the specified region */ function countriesOfRegion(regionSlug) { const region = regionSlug.toLowerCase(); const data = regions_1.regions.find(r => r.slug === region); if (!data) { return []; } return countries_1.countries.filter(country => country.region.toLowerCase() === region); } /** * Get countries by subregion * @param subregion - The name of the subregion to filter by * @returns Array of countries in the specified subregion */ function countriesOfSubRegion(subregion) { const normalizedSubRegion = subregion.toLowerCase(); return countries_1.countries.filter(country => country.subregion.toLowerCase() === normalizedSubRegion); } /** * Find a country by its code (2-letter ISO code) * @param code - The 2-letter ISO code of the country * @returns The country object or undefined if not found */ function countryByISO2(code) { const iso2 = code.toUpperCase(); return countries_1.countries.find(country => country.iso2 === iso2); } /** * Find a country by its 3-letter ISO code * @param code3 - The 3-letter ISO code of the country * @returns The country object or undefined if not found */ function countryByISO3(code3) { const iso3 = code3.toUpperCase(); return countries_1.countries.find(country => country.iso3 === iso3); } /** * Find a country by its slug * @param slug - The slug of the country * @returns The country object or undefined if not found */ function countryBySlug(slug) { const countrySlug = slug.toLowerCase(); return countries_1.countries.find(country => country.slug === countrySlug); } /** * Search countries by name * @param query - The search query * @returns Array of countries that match the search query */ function countryByName(query) { const name = query.toLowerCase(); return countries_1.countries.filter(country => country.name.toLowerCase().includes(name)); } /** * Find a region by its slug * @param slug - The slug of the region * @returns The region object or undefined if not found */ function regionBySlug(slug) { const regionSlug = slug.toLowerCase(); return regions_1.regions.find(region => region.slug === regionSlug); } /** * Search regions by its name * @param query - The slug of the region * @returns The array of regions that match the search query */ function regionsByName(query) { const name = query.toLowerCase(); return regions_1.regions.filter(region => region.name.toLowerCase().includes(name)); }