Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Fixes issue with partial matches. #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/global_phone/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ class Format < Record

def match(national_string, match_leading_digits = true)
return false if match_leading_digits && leading_digits && national_string !~ leading_digits
national_string =~ pattern
if match = pattern.match(national_string)
return match[0].length == national_string.length
end
false
end

def format_replacement_string(type)
Expand Down
6 changes: 5 additions & 1 deletion lib/global_phone/number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ def international_format
end

def valid?
!!(format && national_string =~ national_pattern)
return unless !!format
if match = national_pattern.match(national_string)
return match[0] == national_string
end
false
end

def inspect
Expand Down
21 changes: 21 additions & 0 deletions test/number_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,26 @@ class NumberTest < TestCase
number = context.parse("(312) 555-1212")
assert_equal "+1 312-555-1212", number.international_format
end

test "partial matches do not validate" do
# Guatemala has eight digit phone numbers, but the number 5555
# 1234 123 still got through the matcher.
#
# The regex is /^[2-7]\d{7}|1[89]\d{9}$/, but with =~ matching a
# partial match is still possible.
#
# I think this might actually be a broken regex, but changing
# the code seems better than mucking about with all the regex's
# from google.
number = context.parse("5555 1234 123", :gt)
assert !number || !number.valid?
end

test "national format with or in regex still works with correct number" do
number = context.parse("5555 2134", :gt)
assert number.valid?
end


end
end