From bb0e45937774c318d6a904094b652ce59a197c1d Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Fri, 26 Jul 2024 11:24:00 +0100 Subject: [PATCH] Fall back to by population when there are cities with the same name in different time zones Refs #31 --- src/lib/Iso3166.ts | 18 ++++++++++++++---- test/Iso3166.test.ts | 1 + 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/lib/Iso3166.ts b/src/lib/Iso3166.ts index 1633e5c..3fe9d1c 100644 --- a/src/lib/Iso3166.ts +++ b/src/lib/Iso3166.ts @@ -35,11 +35,21 @@ export const guessCountry: (location: string) => Option.Option = 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, ), diff --git a/test/Iso3166.test.ts b/test/Iso3166.test.ts index 2d0bf08..0df23ad 100644 --- a/test/Iso3166.test.ts +++ b/test/Iso3166.test.ts @@ -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'],