Skip to content

Commit

Permalink
Recognise major cities
Browse files Browse the repository at this point in the history
Refs #31
  • Loading branch information
thewilkybarkid committed Jul 11, 2024
1 parent 6d5fc9b commit 8cfe993
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@effect/schema": "^0.68.1",
"@js-temporal/polyfill": "^0.4.4",
"@observablehq/framework": "^1.9.0",
"@vvo/tzdb": "^6.141.0",
"d3-dsv": "^3.0.1",
"d3-time-format": "^4.1.0",
"diacritics": "^1.3.0",
Expand Down
12 changes: 12 additions & 0 deletions src/lib/Iso3166.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Schema } from '@effect/schema'
import { rawTimeZones } from '@vvo/tzdb'
import diacritics from 'diacritics'
import { Array, Option, type Predicate, String, flow } from 'effect'
import iso3166 from 'i18n-iso-countries'
Expand Down Expand Up @@ -27,6 +28,17 @@ export const guessCountry: (location: string) => Option.Option<Alpha2Code> = flo
subDivision => subDivision.country,
),
),
Option.orElse(() =>
Option.map(
Array.findFirst(rawTimeZones, timezone =>
Array.some(
timezone.mainCities,
mainCity => diacritics.remove(location).toLowerCase() === diacritics.remove(mainCity).toLowerCase(),
),
),
timeZone => timeZone.countryCode,
),
),
),
),
Option.filter(isAlpha2Code),
Expand Down
4 changes: 2 additions & 2 deletions test/Iso3166.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ describe('guessCountry', () => {
['Czech Republic', 'CZ'],
['Czechia', 'CZ'],
['GHANA', 'GH'],
['London', 'GB'],
['London, Ontario', 'CA'],
['London, UK', 'GB'],
['London, United Kingdom', 'GB'],
['New York City and Los Angeles', 'US'],
['Québec', 'CA'],
['Rio de Janeiro, Brazil.', 'BR'],
['Sunnyvale, California', 'US'],
Expand All @@ -31,9 +33,7 @@ describe('guessCountry', () => {
test.for([
'Beijing University of Technology, Beijing 100124, PR China',
'Fayetteville GA (near Atlanta)',
'London',
'Mars',
'New York City and Los Angeles',
'Southeast Asia',
'The UK',
])("doesn't guess %s", input => {
Expand Down

0 comments on commit 8cfe993

Please sign in to comment.