Skip to content

Commit

Permalink
Handle & in country names as per #45
Browse files Browse the repository at this point in the history
  • Loading branch information
meeDamian committed Aug 29, 2024
1 parent 4442dc9 commit 258646f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ export const FLAG_RE = /\uD83C[\uDDE6-\uDDFF]/;

const NAME_SEP = ', ';

function normalizeName(name) {
if (!name || !name.includes(NAME_SEP)) {
export function normalizeName(name) {
if (!name) {
return name;
}

name = name.replace(/\s*&\s*/g, ' and ');

if (!name.includes(NAME_SEP)) {
return name;
}

Expand All @@ -27,12 +33,14 @@ export function fuzzyCompare(input, name) {
}

const normalizedName = normalizeName(name);
const normalizedInput = normalizeName(input);

// Cases like:
// "British Virgin Islands" <-> "Virgin Islands, British"
// "Republic of Moldova" <-> "Moldova, Republic of"
if (normalizedName !== name) {
if (normalizedName.includes(input) || input.includes(normalizedName)) {
// "Trinidad & Tobago" <-> "Trinidad and Tobago"
if (normalizedName !== name || normalizedInput !== input) {
if (normalizedName.includes(normalizedInput) || normalizedInput.includes(normalizedName)) {
return true;
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ test('converts if name within string', t => {
t.is(iso3166, 'CA', 'Should return CA for string containing Canada');
});

test('converts when `&` used in place of `and`', t => {
const iso3166 = code('Trinidad & Tobago');
t.is(iso3166, 'TT', 'Should return TT for Trinidad and Tobago');
});

test('fails on name conflict', t => {
const iso3166 = code('United');
t.is(iso3166, undefined, 'Should return undefined for name conflict');
Expand Down
5 changes: 5 additions & 0 deletions test/flag.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ test('converts if name within string', t => {
t.is(emoji, '🇨🇳', 'Should return 🇨🇳 for string containing China');
});

test('converts when `&` used in place of `and`', t => {
const emoji = flag('Trinidad & Tobago');
t.is(emoji, '🇹🇹', 'Should return 🇹🇹 for Trinidad and Tobago');
});

test('fails on name conflict', t => {
const emoji = flag('Korea');
t.is(emoji, undefined, 'Should return undefined for name conflict Korea');
Expand Down

0 comments on commit 258646f

Please sign in to comment.