Skip to content

Commit

Permalink
Guess country names with full stops
Browse files Browse the repository at this point in the history
Refs #31, 6e98a7f
  • Loading branch information
thewilkybarkid committed Jul 11, 2024
1 parent f866f53 commit 08a2127
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 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 } from 'effect'
import { Option, type Predicate, String, flow } from 'effect'
import iso3166 from 'i18n-iso-countries'

export type Alpha2Code = iso3166.Alpha2Code
Expand All @@ -9,5 +9,8 @@ export const isAlpha2Code: Predicate.Refinement<unknown, Alpha2Code> = (u): u is

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

export const guessCountry = (location: string): Option.Option<Alpha2Code> =>
Option.fromNullable(iso3166.getAlpha2Code(location, 'en')).pipe(Option.filter(isAlpha2Code))
export const guessCountry: (location: string) => Option.Option<Alpha2Code> = flow(
String.replaceAll('.', ''),
Option.liftNullable(location => iso3166.getAlpha2Code(location, 'en')),
Option.filter(isAlpha2Code),
)
2 changes: 1 addition & 1 deletion test/Iso3166.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('guessCountry', () => {
['Czechia', 'CZ'],
['GHANA', 'GH'],
['UK', 'GB'],
['U.K.', 'GB'],
['United Kingdom', 'GB'],
['united states', 'US'],
])('guesses %s', ([input, expected]) => {
Expand All @@ -34,7 +35,6 @@ describe('guessCountry', () => {
'Southeast Asia',
'Sunnyvale, California',
'The UK',
'U.K.',
'Washington, DC',
])("doesn't guess %s", input => {
const actual = _.guessCountry(input)
Expand Down

0 comments on commit 08a2127

Please sign in to comment.