Skip to content

Commit

Permalink
Merge pull request #3 from AidanKneller/main
Browse files Browse the repository at this point in the history
Handle when a unit prefix is absent
  • Loading branch information
zerodahero authored Feb 22, 2024
2 parents a0f1f1c + 94bcaee commit cb48d58
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private function setupRegularExpressions()

$this->zip_regexp = '(\d{5})(?:-?(\d{4})?)';
$this->corner_regexp = '(?:\band\b|\bat\b|&|\@)';
$this->unit_regexp = '(?:(su?i?te|p\W*[om]\W*b(?:ox)?|dept|apt|apartment|ro*m|fl|unit|box)\W+|\#\W*)([\w-]+)';
$this->unit_regexp = '(?:(su?i?te|p\W*[om]\W*b(?:ox)?|dept|apt|apartment|ro*m|fl|unit|box)\W+|\#\W*|)([\w-]+)';
$this->street_regexp =
'(?:'
. '(?:(' . $this->direct_regexp . ')\W+'
Expand Down
44 changes: 44 additions & 0 deletions tests/NormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,48 @@ public function testFailsOnBadAddresses($badAddress)

$this->assertFalse($normalizer->parse($badAddress));
}

/** @test */
public function testHandlesAddressWithoutUnitPrefix()
{
$normalizer = new Normalizer();

$addresses = [
[ // Test without unit prefix
'test' => '1234 W Main Avenue 1W, Chicago, IL, 60647',
'expected_result' => '1234 W Main Ave #1W, Chicago, IL 60647'
],
[ // Regression test with unit prefix
'test' => '1234 W Main Avenue Unit 1W, Chicago, IL, 60647',
'expected_result' => '1234 W Main Ave Unit 1W, Chicago, IL 60647'
],
[ // Regression test with unit prefix
'test' => '1234 W Main Avenue Apartment 1W, Chicago, IL, 60647',
'expected_result' => '1234 W Main Ave Apartment 1W, Chicago, IL 60647'
],
[ // Regression test with unit prefix
'test' => '1234 W Main Avenue #1W, Chicago, IL, 60647',
'expected_result' => '1234 W Main Ave #1W, Chicago, IL 60647'
],
[ // Regression test with unit prefix
'test' => '1234 W Main Avenue Room 1, Chicago, IL, 60647',
'expected_result' => '1234 W Main Ave Room 1, Chicago, IL 60647'
],
[ // Regression test with unit prefix
'test' => '1234 W Main Avenue Apt 1W, Chicago, IL, 60647',
'expected_result' => '1234 W Main Ave Apt 1W, Chicago, IL 60647'
],
[ // Regression test without any unit
'test' => '1234 W Main Street, Chicago, IL, 60647',
'expected_result' => '1234 W Main St, Chicago, IL 60647'
],
];

foreach ($addresses as $address) {
$this->assertEquals(
$address['expected_result'],
(string)$normalizer->parse($address['test'])
);
}
}
}

0 comments on commit cb48d58

Please sign in to comment.