From d2d425e9044dc5bbfd7b74887ff00cd6c0ce761a Mon Sep 17 00:00:00 2001 From: Aidan Kneller Date: Thu, 1 Feb 2024 10:49:24 -0600 Subject: [PATCH 1/2] allow for unit prefix absence --- src/Normalizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Normalizer.php b/src/Normalizer.php index 9e21d0e..85e29e4 100644 --- a/src/Normalizer.php +++ b/src/Normalizer.php @@ -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+' From 94bcaee93db3364ab8da3986e0c22729ba529518 Mon Sep 17 00:00:00 2001 From: Aidan Kneller Date: Thu, 1 Feb 2024 11:49:29 -0600 Subject: [PATCH 2/2] added unit tests to test new regex change and regression test behavior --- tests/NormalizerTest.php | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/NormalizerTest.php b/tests/NormalizerTest.php index e8ecf2c..7e00f02 100644 --- a/tests/NormalizerTest.php +++ b/tests/NormalizerTest.php @@ -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']) + ); + } + } }