Skip to content

Commit

Permalink
Replace part with regex
Browse files Browse the repository at this point in the history
  • Loading branch information
RoanPaulus committed Dec 19, 2024
1 parent 2c1213a commit 71a92cd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
9 changes: 9 additions & 0 deletions src/universal/helpers/bag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ describe('getLatLonByAddress', () => {
});
});

test('With leading apostrophe', () => {
expect(extractAddress("'t Dijkhuis 40")).toStrictEqual({
openbareruimteNaam: 't Dijkhuis',
huisnummer: 40,
huisnummertoevoeging: undefined,
huisletter: undefined,
});
});

test('Ignores postcal code, city name and random charcters', () => {
expect(
extractAddress('Straatnaam 1, 1015BA, Amsterdam _ ; ,')
Expand Down
33 changes: 7 additions & 26 deletions src/universal/helpers/bag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function extractAddress(rawAddress: string): BAGQueryParams {
}
words.push(word);
}
const openbareruimteNaam = words.join(' ');

// We know now that we're past the street name so now we can index into the last identifying part.
const houseIdentifier = s[i];
if (!houseIdentifier) {
Expand All @@ -42,7 +42,7 @@ export function extractAddress(rawAddress: string): BAGQueryParams {
splitHuisnummerFromToevoeging(houseIdentifier);

return {
openbareruimteNaam,
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
Expand All @@ -59,31 +59,12 @@ function splitHuisnummerFromToevoeging(
const huisnummertoevoeging = [];
let i = 0;

for (; i < s.length; i++) {
if (!s[i].match(/\d/)) {
break;
}
huisnummer.push(s[i]);
// Matches something like 1, 2-5 or 3F.
const matches = s.match(/^(\d+)-?([\d*|\w*])?$/);
if (!matches) {
throw Error();
}
if (s[i] === '-') {
// Consume the character.
i++;

for (; i < s.length; i++) {
huisnummertoevoeging.push(s[i]);
}
} else {
for (; i < s.length; i++) {
if (!s[i].match(/[a-z]/i)) {
throw Error(
`Unexpected character in huisnummer + toevoeging. Character: '${s[i]}'`
);
}
huisnummertoevoeging.push(s[i]);
}
}

return [huisnummer.join(''), huisnummertoevoeging.join('') || undefined];
return [matches[1], matches[2]];
}

export type BAGSearchAddress = string;
Expand Down

0 comments on commit 71a92cd

Please sign in to comment.