@apollo-tech/country-codes
Version:
A simple utility for retrieving country codes and names
106 lines (104 loc) • 3.33 kB
text/typescript
interface Country {
continent: string;
region: string;
country: string;
capital: string;
fips: string;
iso2: string;
iso3: string;
isoNo: string;
internet: string;
}
type CountryResponse = Country | null;
/**
* Country Codes
* This class is a convenience class to retrieve country codes.
* Use an ISO2, ISO3, ISO number, FIPS, or internet code to retrieve country information.
* You may also use a country name to retrieve country information.
*
* Example:
*
* ```typescript
* const countryCodes = new CountryCodes();
* const country = countryCodes.getCountry('US');
* console.log(country);
* ```
*/
declare class CountryCodes {
/**
* List of countries
*/
countryList: Country[];
/**
* Search for a country by FIPS code
* @param code
*
* @returns CountryResponse
*
* @example
*
* const country = countryCodes.byFips('US');
*/
byFips(code: string): CountryResponse | null;
/**
* Search for a country by ISO code. This method supports ISO2, ISO3 and ISO numeric codes.
* This is determined in the following order:
* 1. typeof code === number -> ISO number
* 2. length === 3 and !isNan(Number(code)) -> ISO number
* 2. code.length === 2 -> ISO2
* 3. code.length === 3 -> ISO3
* If the type cannot be determined, an error is thrown.
* Enable safe mode to suppress this error (returns null).
*
* @param code - ISO code, this is not case-sensitive
* @param safeMode - Enable safe mode to suppress errors
*
* @returns CountryResponse
*
* @example
*
* const country = countryCodes.byIso('US');
* const country = countryCodes.byIso(840);
* const country = countryCodes.byIso('USA', true);
*
*/
byIso(code: string | number, safeMode?: boolean): CountryResponse;
/**
* Search for a country by internet code
* @param code - Internet code, this is not case-sensitive
*
* @returns CountryResponse
*
* @example
*
* const country = countryCodes.byInternet('US');
*/
byInternet(code: string): CountryResponse | null;
/**
* Search for a country by name. This method is case-sensitive by default.
* However, you can enable ignoreCase to perform a case-insensitive search.
* This will also trim the whitespace from the search query to increase the chances of a match.
*
* @param country - Country name to search for
* @param ignoreCase - Enable case-insensitive search
*
* @returns CountryResponse
*
* @example
*
* const country = countryCodes.byCountry('United States');
*
* const countryWithCaseInsensitive = countryCodes.byCountry('united states', true);
*/
byCountry(country: string, ignoreCase?: boolean): CountryResponse | null;
/**
* Search for a matching country based on the field and "code" (data input) provided.
* This method is used internally by the public methods to search for a country.
* @param field - The field to match on
* @param code - The value to match
* @param ignoreCase - Enable case-insensitive search
* @private
*/
private search;
}
export { type Country, type CountryResponse, CountryCodes as default };