@aleksejdix/ally-bcp47
Version:
TypeScript package for working with BCP-47 language tags
158 lines • 4.25 kB
JavaScript
/**
* ISO 3166 country/region codes registry
* This file contains data for validating region subtags against the official registry
*/
/**
* Map of valid ISO 3166-1 alpha-2 country codes
* Key: country code, Value: country name
*/
export const ISO_3166_REGIONS = {
// European countries
AT: "Austria",
BE: "Belgium",
BG: "Bulgaria",
HR: "Croatia",
CY: "Cyprus",
CZ: "Czech Republic",
DK: "Denmark",
EE: "Estonia",
FI: "Finland",
FR: "France",
DE: "Germany",
GR: "Greece",
HU: "Hungary",
IE: "Ireland",
IT: "Italy",
LV: "Latvia",
LT: "Lithuania",
LU: "Luxembourg",
MT: "Malta",
NL: "Netherlands",
PL: "Poland",
PT: "Portugal",
RO: "Romania",
SK: "Slovakia",
SI: "Slovenia",
ES: "Spain",
SE: "Sweden",
// Other European countries
GB: "United Kingdom",
CH: "Switzerland",
NO: "Norway",
IS: "Iceland",
LI: "Liechtenstein",
MC: "Monaco",
AD: "Andorra",
SM: "San Marino",
VA: "Vatican City",
AL: "Albania",
BA: "Bosnia and Herzegovina",
ME: "Montenegro",
MK: "North Macedonia",
RS: "Serbia",
MD: "Moldova",
UA: "Ukraine",
BY: "Belarus",
RU: "Russia",
TR: "Turkey",
GE: "Georgia",
AM: "Armenia",
AZ: "Azerbaijan",
// Major world regions
US: "United States",
CA: "Canada",
MX: "Mexico",
BR: "Brazil",
AR: "Argentina",
AU: "Australia",
NZ: "New Zealand",
CN: "China",
JP: "Japan",
KR: "South Korea",
IN: "India",
ID: "Indonesia",
MY: "Malaysia",
SG: "Singapore",
TH: "Thailand",
VN: "Vietnam",
ZA: "South Africa",
NG: "Nigeria",
EG: "Egypt",
SA: "Saudi Arabia",
AE: "United Arab Emirates",
IL: "Israel",
UZ: "Uzbekistan",
// UN regions
"001": "World",
"002": "Africa",
"019": "Americas",
"142": "Asia",
"150": "Europe",
"009": "Oceania",
// Special regions
"419": "Latin America",
};
/**
* Map of common incorrect region codes to their correct forms
* Key: incorrect code, Value: correct code
*/
export const REGION_CODE_CORRECTIONS = {
UK: "GB", // Common mistake: UK instead of GB for United Kingdom
EU: "150", // EU is not a valid ISO 3166 code, suggest Europe (150)
EN: "GB", // Common mistake: English language code instead of GB
LA: "419", // LA is sometimes used for Latin America, correct is 419
};
/**
* Map of preferred values for region codes
* According to BCP-47 canonicalization rules
*/
export const REGION_PREFERRED_VALUES = {
// Deprecated region codes with preferred replacements
BU: "MM", // Burma -> Myanmar
CS: "RS", // Serbia and Montenegro -> Serbia
DD: "DE", // East Germany -> Germany
FX: "FR", // Metropolitan France -> France
TP: "TL", // East Timor -> Timor-Leste
YU: "RS", // Yugoslavia -> Serbia
ZR: "CD", // Zaire -> Democratic Republic of the Congo
};
/**
* Checks if a region code is valid according to ISO 3166
*
* @param code The region code to validate
* @returns True if the code is valid, false otherwise
*/
export function isValidRegionCode(code) {
return code.toUpperCase() in ISO_3166_REGIONS;
}
/**
* Gets a suggestion for an invalid region code
*
* @param code The invalid region code
* @returns A suggested correction or undefined if no suggestion is available
*/
export function getSuggestedRegionCode(code) {
return REGION_CODE_CORRECTIONS[code.toUpperCase()];
}
/**
* Checks if a region code has a preferred value in the registry
*
* @param code The region code to check
* @returns True if the code has a preferred value, false otherwise
*/
export function hasPreferredRegionValue(code) {
return code.toUpperCase() in REGION_PREFERRED_VALUES;
}
/**
* Gets the preferred value for a region code
*
* @param code The region code to get the preferred value for
* @returns The preferred value or the original code if no preferred value exists
*/
export function getRegionPreferredValue(code) {
const upperCode = code.toUpperCase();
return upperCode in REGION_PREFERRED_VALUES
? REGION_PREFERRED_VALUES[upperCode]
: upperCode;
}
//# sourceMappingURL=iso3166.js.map