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

Fix: regex error with leading_digits #50

Open
wants to merge 3 commits 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
2 changes: 1 addition & 1 deletion lib/global_phone/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module GlobalPhone
class Format < Record
field 0, :pattern do |p| /^#{p}$/ end
field 1, :national_format_rule
field 2, :leading_digits do |d| /^#{d}/ end
field 2, :leading_digits do |d| /^(#{d})/ end
field 3, :national_prefix_formatting_rule
field 4, :international_format_rule, :fallback => :national_format_rule

Expand Down
3 changes: 2 additions & 1 deletion lib/global_phone/number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def apply_national_prefix_format(result)

prefix = prefix.gsub("$NP", national_prefix)
prefix = prefix.gsub("$FG", match[1])
result = "#{prefix} #{match[2]}"

result = "#{prefix}#{match[2]}"
end

def national_prefix_formatting_rule
Expand Down
2 changes: 1 addition & 1 deletion test/edge_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class EdgeTest < TestCase
# We don't include those formats in our database, so we fall
# back to the closest match.
number = context.parse("1520123456", "IE")
assert_equal "1520 123 456", number.national_format
assert_equal "1520 123 456", number.national_format
end
end
end
16 changes: 16 additions & 0 deletions test/format_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require File.expand_path('../test_helper', __FILE__)

module GlobalPhone
class FormatTest < TestCase
test 'non-matching leading digits' do
assert_false format.match('1582380560', true)
end

def format
# This is a GB format taken from the seed data.
Format.new(["(\\d{2})(\\d{4})(\\d{4})",
"$1 $2 $3",
"2|5[56]|7(?:0|6[013-9])2|5[56]|7(?:0|6(?:[013-9]|2[0-35-9]))"])
end
end
end