diff --git a/src/universal/helpers/bag.test.ts b/src/universal/helpers/bag.test.ts index 02875d1b6..be2ee438d 100644 --- a/src/universal/helpers/bag.test.ts +++ b/src/universal/helpers/bag.test.ts @@ -174,13 +174,31 @@ describe('getLatLonByAddress', () => { test('With leading apostrophe', () => { expect(extractAddress("'t Dijkhuis 40")).toStrictEqual({ - openbareruimteNaam: 't Dijkhuis', + openbareruimteNaam: "'t Dijkhuis", huisnummer: 40, huisnummertoevoeging: undefined, huisletter: undefined, }); }); + test('With a slash in the address', () => { + expect(extractAddress('PATER V/D ELSENPLEIN 86')).toStrictEqual({ + openbareruimteNaam: 'PATER V/D ELSENPLEIN', + huisnummer: 86, + huisnummertoevoeging: undefined, + huisletter: undefined, + }); + }); + + test('With a dot in the address', () => { + expect(extractAddress('P/A ST. JACOBSLAAN 339')).toStrictEqual({ + openbareruimteNaam: 'P/A ST. JACOBSLAAN', + huisnummer: 339, + huisnummertoevoeging: undefined, + huisletter: undefined, + }); + }); + test('Ignores postcal code, city name and random charcters', () => { expect( extractAddress('Straatnaam 1, 1015BA, Amsterdam _ ; ,') diff --git a/src/universal/helpers/bag.ts b/src/universal/helpers/bag.ts index 1cbedb9f6..ecf692755 100644 --- a/src/universal/helpers/bag.ts +++ b/src/universal/helpers/bag.ts @@ -2,12 +2,9 @@ import { LatLngLiteral, LatLngTuple } from 'leaflet'; import { BAGQueryParams, BAGAdreseerbaarObject } from '../types/bag'; -// Quick and dirty see also: https://stackoverflow.com/a/68401047 export function extractAddress(rawAddress: string): BAGQueryParams { - // Strip down to Street + Housenumber - const address = rawAddress - // Remove everything but alphanumeric, dash, dot and space - .replace(/[^0-9-.\s\p{Script=Latin}+]/giu, ''); + // Remove everything but alphanumeric, dash, dot, apostrophe and space. + const address = rawAddress.replace(/[^/'0-9-.\s\p{Script=Latin}+]/giu, ''); const words = []; const s = address.split(' '); @@ -45,9 +42,8 @@ export function extractAddress(rawAddress: string): BAGQueryParams { openbareruimteNaam: words.join(' '), huisnummer: parseInt(huisnummer), huisnummertoevoeging, - // RP TODO: What is huisletter in the query? I already added one with a letter but this counts - // as toevoeging. - // There is also mention of a dot in replacing trash characters. What to do when there is a dot? + // Leave out huisletter. This is used to look up a location on the map, + // and it's okay to show an approximate location. huisletter: undefined, }; } @@ -60,9 +56,11 @@ function splitHuisnummerFromToevoeging( let i = 0; // Matches something like 1, 2-5 or 3F. - const matches = s.match(/^(\d+)-?([\d*|\w*])?$/); + const matches = s.match(/(\d+)-?(\d*|\w*)?/); if (!matches) { - throw Error(); + throw Error( + `Match failed for housenumber and/or toevoeging. Input string: '${s}'` + ); } return [matches[1], matches[2]]; }