Skip to content

Commit 5c5d594

Browse files
committed
fixed issue #152, incorrect formatting of MX
1 parent 39a71cd commit 5c5d594

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/phonelib/phone_formatter.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,12 @@ def formatting_data
149149
rule.gsub!(/(\$NP|\$FG)/, '$NP' => prefix, '$FG' => '$1')
150150

151151
# add space to format groups, change first group to rule,
152-
format_string = format[:format].gsub(/(\d)\$/, '\\1 $').gsub('$1', rule)
152+
format_string = format[:format].gsub(/(\d)\$/, '\\1 $')
153+
if format_string.include? '$1'
154+
format_string.gsub! '$1', rule
155+
else
156+
format_string = rule.gsub('$1', '') + format_string
157+
end
153158

154159
@formatting_data =
155160
[@national_number.match(/#{format[Core::PATTERN]}/), format_string]

spec/phonelib_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@
454454
number = Phonelib.parse('+5215545258448', 'mx')
455455
expect(number.valid?).to be true
456456
expect(number.international).to eq('+52 1 55 4525 8448')
457-
expect(number.national).to eq('55 4525 8448')
457+
expect(number.national).to eq('044 55 4525 8448')
458458

459459
intl = number.international
460460

@@ -1150,6 +1150,13 @@
11501150
end
11511151
end
11521152

1153+
context 'issue #152' do
1154+
it 'should return correct format for MX' do
1155+
p = Phonelib.parse('0459991234567', 'MX')
1156+
expect(p.national).to eq('044 999 123 4567')
1157+
end
1158+
end
1159+
11531160
context 'example numbers' do
11541161
it 'are valid' do
11551162
data_file = File.dirname(__FILE__) + '/../data/phone_data.dat'

0 commit comments

Comments
 (0)