Skip to content

Commit

Permalink
fixed issue #304 - fix for national prefix replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
daddyz committed Jul 17, 2024
1 parent bef1e3b commit ceed2e5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/phonelib/phone_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ def better_result(base_result, result = nil)
# replacing national prefix to simplified format
def with_replaced_national_prefix(phone, data)
return phone unless data[Core::NATIONAL_PREFIX_TRANSFORM_RULE]
phone = phone.gsub(/^#{data[Core::COUNTRY_CODE]}/, '') if phone.start_with?(data[Core::COUNTRY_CODE])
pattern = cr("^(?:#{data[Core::NATIONAL_PREFIX_FOR_PARSING]})")
match = phone.match pattern
if match && match.captures.compact.size > 0
phone.gsub(pattern, data[Core::NATIONAL_PREFIX_TRANSFORM_RULE])
data[Core::COUNTRY_CODE] + phone.gsub(pattern, data[Core::NATIONAL_PREFIX_TRANSFORM_RULE])
else
phone
end
Expand Down
10 changes: 10 additions & 0 deletions spec/phonelib_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,16 @@
end
end

context 'issue #304' do
it 'should strip AR prefix' do
phone = Phonelib.parse('+540111557447700', 'AR')
expect(phone.valid_for_country?('AR')).to be(true)
expect(phone.e164).to eq('+5491157447700')
expect(phone.national).to eq('011 15-5744-7700')
expect(phone.international).to eq('+54 9 11 5744-7700')
end
end

context 'example numbers' do
it 'are valid' do
data_file = File.dirname(__FILE__) + '/../data/phone_data.dat'
Expand Down

0 comments on commit ceed2e5

Please sign in to comment.