Skip to content

Commit

Permalink
Guess reasonably sized settlements
Browse files Browse the repository at this point in the history
Refs #31
  • Loading branch information
thewilkybarkid committed Jul 11, 2024
1 parent 31bf6b9 commit fc0f070
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 14 deletions.
61 changes: 60 additions & 1 deletion package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@js-temporal/polyfill": "^0.4.4",
"@observablehq/framework": "^1.9.0",
"@vvo/tzdb": "^6.141.0",
"all-the-cities": "^3.1.0",
"d3-dsv": "^3.0.1",
"d3-time-format": "^4.1.0",
"diacritics": "^1.3.0",
Expand All @@ -20,6 +21,7 @@
"@dotenvx/dotenvx": "^1.5.0",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@tsconfig/node20": "^20.1.4",
"@types/all-the-cities": "^3.1.3",
"@types/diacritics": "^1.3.3",
"@types/node": "^20.14.9",
"@typescript-eslint/eslint-plugin": "^7.14.1",
Expand Down
12 changes: 12 additions & 0 deletions src/lib/Iso3166.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Schema } from '@effect/schema'
import { rawTimeZones } from '@vvo/tzdb'
import cities from 'all-the-cities'
import diacritics from 'diacritics'
import { Array, Option, type Predicate, String, flow } from 'effect'
import iso3166 from 'i18n-iso-countries'
Expand All @@ -12,6 +13,8 @@ export const isAlpha2Code: Predicate.Refinement<unknown, Alpha2Code> = (u): u is

export const Alpha2CodeSchema: Schema.Schema<Alpha2Code, string> = Schema.String.pipe(Schema.filter(isAlpha2Code))

const cities10000 = Array.filter(cities, city => city.population >= 10_000)

export const guessCountry: (location: string) => Option.Option<Alpha2Code> = flow(
String.replaceAll('.', ''),
String.replaceAll(/\((.+?)\)/g, ', $1'),
Expand Down Expand Up @@ -40,6 +43,15 @@ export const guessCountry: (location: string) => Option.Option<Alpha2Code> = flo
timeZone => timeZone.countryCode,
),
),
Option.orElse(() =>
Option.map(
Array.findFirst(
cities10000,
city => diacritics.remove(location).toLowerCase() === diacritics.remove(city.name).toLowerCase(),
),
city => city.country,
),
),
),
),
Option.filter(isAlpha2Code),
Expand Down
22 changes: 9 additions & 13 deletions test/Iso3166.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ describe('guessCountry', () => {
['Colorado - United States', 'US'],
['Czech Republic', 'CZ'],
['Czechia', 'CZ'],
['Düsseldorf', 'DE'],
['GHANA', 'GH'],
['London', 'GB'],
['London, Ontario', 'CA'],
['London, UK', 'GB'],
['London, United Kingdom', 'GB'],
['New York City and Los Angeles', 'US'],
['Norwich', 'GB'],
['Québec', 'CA'],
['Rio de Janeiro, Brazil.', 'BR'],
['Sunnyvale, California', 'US'],
Expand All @@ -31,18 +33,12 @@ describe('guessCountry', () => {
expect(actual).toStrictEqual(Option.some(expected))
})

test.for([
'Babol iran',
'Düsseldorf',
'Fayetteville GA (near Atlanta)',
'Mars',
'Norwich',
'Southeast Asia',
'The UK',
'Yale university',
])("doesn't guess %s", input => {
const actual = _.guessCountry(input)
test.for(['Babol iran', 'Fayetteville GA (near Atlanta)', 'Mars', 'Southeast Asia', 'The UK', 'Yale university'])(
"doesn't guess %s",
input => {
const actual = _.guessCountry(input)

expect(actual).toStrictEqual(Option.none())
})
expect(actual).toStrictEqual(Option.none())
},
)
})

0 comments on commit fc0f070

Please sign in to comment.