-
Notifications
You must be signed in to change notification settings - Fork 25
/
lib.js
188 lines (152 loc) · 4.59 KB
/
lib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import countries from '../countries.json' with { type: 'json' };
export const MAGIC_NUMBER = 127_462 - 65; // Base offset for flag emoji calculation
export const CODE_RE = /^[a-z]{2}$/i;
export const NAME_RE = /^.{2,}$/;
export const FLAG_RE = /\uD83C[\uDDE6-\uDDFF]/;
const NAME_SEP = ', ';
// Turn returned names from ex. "Virgin Islands, British" into "British Virgin Islands"
export function normalizeOutput(name) {
if (!name) {
return name;
}
if (name.includes(NAME_SEP)) {
name = name.split(NAME_SEP).reverse().join(' ');
}
return name;
}
// For all non-empty strings, strip out diacritics, replace & with 'and', and turn "weird form" in
export function normalizeName(name) {
if (!name) {
return name;
}
// Replace & with and
name = name.replaceAll(/\s*&\s*/g, ' and ');
// Replace diacritics with their base characters
name = name.normalize('NFD').replaceAll(/[\u0300-\u036F]/g, '');
name = normalizeOutput(name);
return name;
}
// Compare two names, allowing for differences
export function fuzzyCompare(input, name) {
name = name.toLowerCase();
// Cases like:
// "Vatican" <-> "Holy See (Vatican City State)"
// "Russia" <-> "Russian Federation"
if (name.includes(input) || input.includes(name)) {
return true;
}
const normalizedName = normalizeName(name);
const normalizedInput = normalizeName(input);
// Cases like:
// "British Virgin Islands" <-> "Virgin Islands, British"
// "Republic of Moldova" <-> "Moldova, Republic of"
// "Trinidad & Tobago" <-> "Trinidad and Tobago"
if (normalizedName !== name || normalizedInput !== input) {
if (normalizedName.includes(normalizedInput) || normalizedInput.includes(normalizedName)) {
return true;
}
}
return false;
}
export function isCode(code) {
code = code.toUpperCase();
return countries[code] ? code : undefined;
}
export function nameToCode(name) {
if (!name || !NAME_RE.test(name)) {
return;
}
name = name.trim().toLowerCase();
// Look for exact match
for (const [code, names] of Object.entries(countries)) {
if (names.some(n => n.toLowerCase() === name)) {
return code;
}
}
// Look for inexact match
const matches = Object.keys(countries)
.filter(code =>
countries[code].some(n => fuzzyCompare(name, n)),
);
// Return only when exactly one match was found
// prevents cases like "United"
if (matches.length === 1) {
return matches[0];
}
}
export function codeToName(code) {
if (!code || !CODE_RE.test(code)) {
return;
}
return normalizeOutput(countries[code.toUpperCase()]?.[0]);
}
export function codeToFlag(code) {
if (!code || !CODE_RE.test(code)) {
return;
}
code = isCode(code);
if (!code) {
return;
}
return String.fromCodePoint(...[...code].map(c => MAGIC_NUMBER + c.codePointAt(0)));
}
export function flagToCode(flag) {
if (!flag || !FLAG_RE.test(flag)) {
return;
}
return isCode([...flag].map(c => c.codePointAt(0) - MAGIC_NUMBER).map(c => String.fromCodePoint(c)).join(''));
}
/**
* Converts a country flag emoji or country name to its corresponding ISO 3166-1 alpha-2 country code.
*
* @param {string} input - A country flag emoji or full country name.
* @returns {string|undefined} The country code (e.g., 'US') if found, otherwise `undefined`.
*
* @example
* code('🇨🇦'); // Returns 'CA'
* code('Canada'); // Returns 'CA'
*/
export function code(input) {
return flagToCode(input) || nameToCode(input);
}
/**
* Converts a country code or country name to its corresponding flag emoji.
*
* @param {string} input - A country code (e.g., 'US') or full country name.
* @returns {string|undefined} The country flag emoji (e.g., '🇺🇸') if found, otherwise `undefined`.
*
* @example
* flag('US'); // Returns '🇺🇸'
* flag('United States'); // Returns '🇺🇸'
*/
export function flag(input) {
if (!CODE_RE.test(input) || input === 'UK') {
input = nameToCode(input);
}
return codeToFlag(input);
}
/**
* Converts a country flag emoji or country code to its corresponding country name.
*
* @param {string} input - A country flag emoji or ISO 3166-1 alpha-2 country code.
* @returns {string|undefined} The country name (e.g., 'United States') if found, otherwise `undefined`.
*
* @example
* name('🇺🇸'); // Returns 'United States'
* name('US'); // Returns 'United States'
*/
export function name(input) {
if (FLAG_RE.test(input)) {
input = flagToCode(input);
}
return codeToName(input);
}
/**
* An object containing all country codes mapped to their respective country names and aliases.
*
* @type {Object.<string, string[]>}
*
* @example
* countries['CA']; // Returns ['Canada', 'Canadian']
*/
export {countries};