Skip to content

Commit

Permalink
Added some extra tests and fixed them
Browse files Browse the repository at this point in the history
  • Loading branch information
RoanPaulus committed Dec 19, 2024
1 parent 71a92cd commit 1020185
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
20 changes: 19 additions & 1 deletion src/universal/helpers/bag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 _ ; ,')
Expand Down
21 changes: 10 additions & 11 deletions src/universal/helpers/bag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ');
Expand All @@ -20,10 +17,11 @@ export function extractAddress(rawAddress: string): BAGQueryParams {
}

let i = 0;
const onDigit = new RegExp(/\d/);

for (; i < s.length; i++) {
const word = s[i];
if (word[0].match(/\d/)) {
if (word[0].match(onDigit)) {
// The first housenumber found, so there are no more streetname words left.
break;
}
Expand All @@ -45,9 +43,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,
};
}
Expand All @@ -60,9 +57,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]];
}
Expand Down

0 comments on commit 1020185

Please sign in to comment.