From 53fb87ae209230aeced094dddd6a4e9d75d72007 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Thu, 11 Jul 2024 12:11:37 +0100 Subject: [PATCH] Ignore some words probably not part of place names Refs #31 --- src/lib/Iso3166.ts | 1 + test/Iso3166.test.ts | 13 ++++++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib/Iso3166.ts b/src/lib/Iso3166.ts index 4273dda..1633e5c 100644 --- a/src/lib/Iso3166.ts +++ b/src/lib/Iso3166.ts @@ -20,6 +20,7 @@ export const guessCountry: (location: string) => Option.Option = flo String.replaceAll(/\((.+?)\)/g, ', $1'), String.replaceAll(/ [0-9]+/g, ' '), String.replaceAll(/( and | - )/gi, ', '), + String.replaceAll(/(^| )(the|near) /gi, ''), location => Array.prepend(Array.map(location.split(',').reverse(), String.trim), location), Array.findFirst(location => Option.fromNullable(iso3166.getAlpha2Code(location, 'en')).pipe( diff --git a/test/Iso3166.test.ts b/test/Iso3166.test.ts index 174b5ec..2d0bf08 100644 --- a/test/Iso3166.test.ts +++ b/test/Iso3166.test.ts @@ -12,6 +12,7 @@ describe('guessCountry', () => { ['Czech Republic', 'CZ'], ['Czechia', 'CZ'], ['Düsseldorf', 'DE'], + ['Fayetteville GA (near Atlanta)', 'US'], ['GHANA', 'GH'], ['London', 'GB'], ['London, Ontario', 'CA'], @@ -22,6 +23,7 @@ describe('guessCountry', () => { ['Québec', 'CA'], ['Rio de Janeiro, Brazil.', 'BR'], ['Sunnyvale, California', 'US'], + ['The UK', 'GB'], ['UK', 'GB'], ['U.K.', 'GB'], ['United Kingdom', 'GB'], @@ -33,12 +35,9 @@ describe('guessCountry', () => { expect(actual).toStrictEqual(Option.some(expected)) }) - test.for(['Babol iran', 'Fayetteville GA (near Atlanta)', 'Mars', 'Southeast Asia', 'The UK', 'Yale university'])( - "doesn't guess %s", - input => { - const actual = _.guessCountry(input) + test.for(['Babol iran', 'Mars', 'Southeast Asia', 'Yale university'])("doesn't guess %s", input => { + const actual = _.guessCountry(input) - expect(actual).toStrictEqual(Option.none()) - }, - ) + expect(actual).toStrictEqual(Option.none()) + }) })