Skip to content

Commit

Permalink
Fall back to by population when there are cities with the same name i…
Browse files Browse the repository at this point in the history
…n different time zones

Refs #31
  • Loading branch information
thewilkybarkid committed Jul 26, 2024
1 parent 31134c4 commit bb0e459
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/lib/Iso3166.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,21 @@ export const guessCountry: (location: string) => Option.Option<Alpha2Code> = flo
),
Option.orElse(() =>
Option.map(
Array.findFirst(rawTimeZones, timezone =>
Array.some(
timezone.mainCities,
mainCity => diacritics.remove(location).toLowerCase() === diacritics.remove(mainCity).toLowerCase(),
Array.matchLeft(
Array.filter(rawTimeZones, timezone =>
Array.some(
timezone.mainCities,
mainCity => diacritics.remove(location).toLowerCase() === diacritics.remove(mainCity).toLowerCase(),
),
),
{
onEmpty: () => Option.none(),
onNonEmpty: (head, tail) =>
Array.match(tail, {
onEmpty: () => Option.some(head),
onNonEmpty: Option.none,
}),
},
),
timeZone => timeZone.countryCode,
),
Expand Down
1 change: 1 addition & 0 deletions test/Iso3166.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('guessCountry', () => {
['Québec', 'CA'],
['Rio de Janeiro, Brazil.', 'BR'],
['Sunnyvale, California', 'US'],
['Sydney', 'AU'],
['The UK', 'GB'],
['UK', 'GB'],
['U.K.', 'GB'],
Expand Down

0 comments on commit bb0e459

Please sign in to comment.