From ae92acaad0a8139821ee05d3049243841658bc4c Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Thu, 11 Jul 2024 11:01:40 +0100 Subject: [PATCH] Recognise multiple delimiters Refs #31 --- src/lib/Iso3166.ts | 2 ++ test/Iso3166.test.ts | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib/Iso3166.ts b/src/lib/Iso3166.ts index d796ec5..de36d08 100644 --- a/src/lib/Iso3166.ts +++ b/src/lib/Iso3166.ts @@ -11,6 +11,8 @@ export const Alpha2CodeSchema: Schema.Schema = Schema.String export const guessCountry: (location: string) => Option.Option = 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), diff --git a/test/Iso3166.test.ts b/test/Iso3166.test.ts index 409110d..b0d8cef 100644 --- a/test/Iso3166.test.ts +++ b/test/Iso3166.test.ts @@ -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'], @@ -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',