Skip to content

Commit

Permalink
Guess country names that are somewhere in the string
Browse files Browse the repository at this point in the history
Refs #31, 6e98a7f
  • Loading branch information
thewilkybarkid committed Jul 11, 2024
1 parent 08a2127 commit dc13542
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/lib/Iso3166.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Schema } from '@effect/schema'
import { Option, type Predicate, String, flow } from 'effect'
import { Array, Option, type Predicate, String, flow } from 'effect'
import iso3166 from 'i18n-iso-countries'

export type Alpha2Code = iso3166.Alpha2Code
Expand All @@ -11,6 +11,7 @@ export const Alpha2CodeSchema: Schema.Schema<Alpha2Code, string> = Schema.String

export const guessCountry: (location: string) => Option.Option<Alpha2Code> = flow(
String.replaceAll('.', ''),
Option.liftNullable(location => iso3166.getAlpha2Code(location, 'en')),
location => Array.prepend(Array.map(location.split(',').reverse(), String.trim), location),
Array.findFirst(Option.liftNullable(location => iso3166.getAlpha2Code(location, 'en'))),
Option.filter(isAlpha2Code),
)
8 changes: 4 additions & 4 deletions test/Iso3166.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import * as _ from '../src/lib/Iso3166.js'

describe('guessCountry', () => {
test.for([
['Chicago, IL, USA', 'US'],
['Czech Republic', 'CZ'],
['Czechia', 'CZ'],
['GHANA', 'GH'],
['London, UK', 'GB'],
['London, United Kingdom', 'GB'],
['Rio de Janeiro, Brazil.', 'BR'],
['UK', 'GB'],
['U.K.', 'GB'],
['United Kingdom', 'GB'],
Expand All @@ -21,17 +25,13 @@ describe('guessCountry', () => {
'Algiers ( Algeria)',
'Baylor College of Medicine, Houston, Texas',
'Beijing University of Technology, Beijing 100124, PR China',
'Chicago, IL, USA',
'Colorado - United States',
'Fayetteville GA (near Atlanta)',
'London',
'London, Ontario',
'London, UK',
'London, United Kingdom',
'Mars',
'New York City and Los Angeles',
'Québec',
'Rio de Janeiro, Brazil.',
'Southeast Asia',
'Sunnyvale, California',
'The UK',
Expand Down

0 comments on commit dc13542

Please sign in to comment.