Skip to content

Commit

Permalink
Add name normalisation, as I can't imagine when "Virgin Islands, Brit…
Browse files Browse the repository at this point in the history
…ish" returned would be preferred over "British Virgin Islands". Inspired by and closes #19
  • Loading branch information
meeDamian committed Aug 29, 2024
1 parent 9061d45 commit 4442dc9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ export const CODE_RE = /^[a-z]{2}$/i;
export const NAME_RE = /^.{2,}$/;
export const FLAG_RE = /\uD83C[\uDDE6-\uDDFF]/;

const NAME_SEP = ', ';

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

return name.split(NAME_SEP).reverse().join(' ');
}

export function fuzzyCompare(input, name) {
name = name.toLowerCase();

Expand All @@ -16,12 +26,13 @@ export function fuzzyCompare(input, name) {
return true;
}

const normalizedName = normalizeName(name);

// Cases like:
// "British Virgin Islands" <-> "Virgin Islands, British"
// "Republic of Moldova" <-> "Moldova, Republic of"
if (name.includes(',')) {
const reversedName = name.split(', ').reverse().join(' ');
if (reversedName.includes(input) || input.includes(reversedName)) {
if (normalizedName !== name) {
if (normalizedName.includes(input) || input.includes(normalizedName)) {

Check failure on line 35 in src/lib.js

View workflow job for this annotation

GitHub Actions / test (20)

Unexpected `if` as the only statement in a `if` block without `else`.

Check failure on line 35 in src/lib.js

View workflow job for this annotation

GitHub Actions / test (22)

Unexpected `if` as the only statement in a `if` block without `else`.
return true;
}
}
Expand Down Expand Up @@ -67,7 +78,7 @@ export function codeToName(code) {
return;
}

return countries[code.toUpperCase()]?.[0];
return normalizeName(countries[code.toUpperCase()]?.[0]);
}

export function codeToFlag(code) {
Expand All @@ -80,9 +91,7 @@ export function codeToFlag(code) {
return;
}

if (String && String.fromCodePoint) {
return String.fromCodePoint(...[...code].map(c => MAGIC_NUMBER + c.codePointAt(0)));
}
return String.fromCodePoint(...[...code].map(c => MAGIC_NUMBER + c.codePointAt(0)));
}

export function flagToCode(flag) {
Expand Down
5 changes: 5 additions & 0 deletions test/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ test('returns first name from array for code US', t => {
t.is(country, 'United States', 'Should return United States for code US');
});

test('returns normalized name', t => {
const country = name('VG');
t.is(country, 'British Virgin Islands', 'Should return British Virgin Islands for code VG');
});

test('returns correct name for Macedonian country code', t => {
const country = name('MK');
t.is(country, 'Republic of North Macedonia', 'Should return correct name for code MK');
Expand Down

0 comments on commit 4442dc9

Please sign in to comment.