Skip to content

Commit

Permalink
normalize() now has a number of APIs - whichever the caller wishes to…
Browse files Browse the repository at this point in the history
… use
  • Loading branch information
nigelhorne committed Aug 13, 2024
1 parent 798d42c commit 20f5b3e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Revision history for Geo::Coder::Abbreviations

0.09
normalize() now has a number of APIs - whichever the caller wishes to use

0.08 Tue Jul 2 16:56:34 EDT 2024
Allow CACHE_DIR as well as CACHEDIR
Default cache location is now ~/.cache instead of /tmp/cache
Expand Down
34 changes: 33 additions & 1 deletion lib/Geo/Coder/Abbreviations.pm
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,44 @@ sub abbreviate {
Normalize and abbreviate street names - useful for comparisons
print $abbr->normalize({ street => '10 Downing Street' }), "\n"; # prints '10 DOWNING ST'
Can be run as a class method
print Geo::Coder::Abbreviations('1600 Pennsylvania Avenue NW'), "\n"; # prints '1600 Pennsylvia Ave NW'
=cut

sub normalize
{
my $self = shift;
my $street = shift;
my %params;

# Try hard to support whatever API that the user wants to use
if(!ref($self)) {
if(scalar(@_)) {
return(__PACKAGE__->new()->normalize(@_));
} elsif(!defined($self)) {
# Geo::Coder::Abbreviations->normalize()
Carp::croak('Usage: ', __PACKAGE__, '::normalize(street => $street)');
} elsif($self eq __PACKAGE__) {
Carp::croak("Usage: $self", '::normalize(street => $street)');
}
return(__PACKAGE__->new()->normalize($self));
} elsif(ref($self) eq 'HASH') {
return(__PACKAGE__->new()->noralize($self));
} elsif(ref($_[0]) eq 'HASH') {
%params = %{$_[0]};
# } elsif(ref($_[0]) && (ref($_[0] !~ /::/))) {
} elsif(ref($_[0])) {
Carp::croak('Usage: ', __PACKAGE__, '::normalize(street => $street)');
} elsif(scalar(@_) && (scalar(@_) % 2 == 0)) {
%params = @_;
} else {
$params{'street'} = shift;
}

my $street = $params{'street'};

$street = uc($street);
if($street =~ /(.+)\s+(.+)\s+(.+)/) {
Expand Down
3 changes: 2 additions & 1 deletion t/normalize.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use strict;
use warnings;
use Test::Most tests => 4;
use Test::Most tests => 5;
use Test::NoWarnings;

BEGIN {
Expand All @@ -11,3 +11,4 @@ BEGIN {

my $abbr = new_ok('Geo::Coder::Abbreviations');
cmp_ok($abbr->normalize('1600 Pennsylvania Avenue NW'), 'eq', '1600 PENNSYLVANIA AV NW', 'Basic normalize test');
cmp_ok(Geo::Coder::Abbreviations::normalize('street' => '10 Downing Street'), 'eq', '10 DOWNING ST', 'Verify class method');

0 comments on commit 20f5b3e

Please sign in to comment.