Skip to content

Commit

Permalink
Improve UK number formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
DrHyde committed Nov 5, 2011
1 parent cff444c commit c389c63
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
XXXX-XX-XX X.X Fix nasty bug in formatting unallocated UK numbers;
Sint Maarten (SX) moved from +599 5 to +1 721
Sint Maarten (SX) moved from +599 5 to +1 721;
UK formatting improved for 3+7 and 2+8 formats

2011-10-29 1.9 New UK allocations;
Use libphonenumber's data for N::P::Country;
Expand Down
9 changes: 6 additions & 3 deletions lib/Number/Phone/UK.pm
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,12 @@ sub format {
$self :
__PACKAGE__->new($self);
return '+'.country_code().' '.(
$self->areacode() ? $self->areacode().' '.$self->subscriber() :
!$self->is_allocated() ? ( ${$self} =~ /^\+44/ ? substr(${$self}, 3) : substr(${$self}, 1))
: $self->subscriber()
$self->areacode() ? ($self->areacode().' '.(
length($self->subscriber()) == 7 ? substr($self->subscriber(), 0, 3).' '.substr($self->subscriber(), 3) :
length($self->subscriber()) == 8 ? substr($self->subscriber(), 0, 4).' '.substr($self->subscriber(), 4) :
$self->subscriber() )) :
!$self->is_allocated() ? ( ${$self} =~ /^\+44/ ? substr(${$self}, 3) : substr(${$self}, 1)) :
$self->subscriber()
);
}

Expand Down
4 changes: 2 additions & 2 deletions t/01_uk_data.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ my $number = Number::Phone->new('+44 142422 0000');
ok($number->country() eq 'UK', "inherited country() method works");
ok($number->format() eq '+44 1424 220000', "4+6 number formatted OK");
$number = Number::Phone->new('+44 115822 0000');
ok($number->format() eq '+44 115 8220000', "3+7 number formatted OK");
ok($number->format() eq '+44 115 822 0000', "3+7 number formatted OK");
$number = Number::Phone->new('+442 0 8771 2924');
ok($number->format() eq '+44 20 87712924', "2+8 number formatted OK");
ok($number->format() eq '+44 20 8771 2924', "2+8 number formatted OK");
ok($number->areacode() eq '20', "2+8 number has correct area code");
ok($number->subscriber() eq '87712924', "2+8 number has correct subscriber number");
foreach my $method (qw(is_allocated is_geographic is_valid)) {
Expand Down
2 changes: 1 addition & 1 deletion t/900_bugfix-rt48581.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ my $phone = Number::Phone->new(44, '02087712924');

ok($phone->isa('Number::Phone::UK'), "N::P->new(CC, 'nnnn') returns N::P::CC object");

ok($phone->format() eq "+44 20 87712924", "and it's got the right data");
ok($phone->format() eq "+44 20 8771 2924", "and it's got the right data");

eval { Number::Phone->new(44, '02087712924', 'apples!') };
ok($@ =~ /too many params/, "dies OK on too many params");
2 changes: 1 addition & 1 deletion t/902_bugfix-rt61177.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use Number::Phone::Country;

ok(Number::Phone->new("442087712924")->country_code() == 44, "known countries return objects");
ok(Number::Phone->new("+442087712924")->country_code() == 44, "known countries with a + return objects");
ok(Number::Phone->new("+442087712924")->format() eq '+44 20 87712924' , "format() works (sanity check cos it changes later)");
ok(Number::Phone->new("+442087712924")->format() eq '+44 20 8771 2924' , "format() works (sanity check cos it changes later)");

# let's break the UK
$Number::Phone::Country::idd_codes{'44'} = 'MOCK';
Expand Down

0 comments on commit c389c63

Please sign in to comment.