Skip to content

Commit

Permalink
Recognise multiple delimiters
Browse files Browse the repository at this point in the history
Refs #31
  • Loading branch information
thewilkybarkid committed Jul 11, 2024
1 parent dc13542 commit ae92aca
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/lib/Iso3166.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const Alpha2CodeSchema: Schema.Schema<Alpha2Code, string> = Schema.String

export const guessCountry: (location: string) => Option.Option<Alpha2Code> = flow(
String.replaceAll('.', ''),
String.replaceAll(/\((.+?)\)/g, ', $1'),
String.replaceAll(/( and | - )/gi, ', '),
location => Array.prepend(Array.map(location.split(',').reverse(), String.trim), location),
Array.findFirst(Option.liftNullable(location => iso3166.getAlpha2Code(location, 'en'))),
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 @@ -4,7 +4,9 @@ import * as _ from '../src/lib/Iso3166.js'

describe('guessCountry', () => {
test.for([
['Algiers ( Algeria)', 'DZ'],
['Chicago, IL, USA', 'US'],
['Colorado - United States', 'US'],
['Czech Republic', 'CZ'],
['Czechia', 'CZ'],
['GHANA', 'GH'],
Expand All @@ -22,10 +24,8 @@ describe('guessCountry', () => {
})

test.for([
'Algiers ( Algeria)',
'Baylor College of Medicine, Houston, Texas',
'Beijing University of Technology, Beijing 100124, PR China',
'Colorado - United States',
'Fayetteville GA (near Atlanta)',
'London',
'London, Ontario',
Expand Down

0 comments on commit ae92aca

Please sign in to comment.