diff --git a/lib/phony_rails.rb b/lib/phony_rails.rb index e5d45cd..400beb5 100644 --- a/lib/phony_rails.rb +++ b/lib/phony_rails.rb @@ -44,6 +44,11 @@ def self.normalize_number(number, options = {}) if not (number =~ /\A\+/) # if we don't have a + if Phony.plausible?("#{_default_country_number}#{number}") || !Phony.plausible?(number) || country_code_from_number(number).nil? number = "#{_default_country_number}#{number}" + elsif (number =~ /^0[^0]/) && Phony.plausible?("#{_default_country_number}#{number.gsub(/^0/, '')}") + # If the number starts with ONE zero (two might indicate a country code) + # and this is a plausible number for the default_country + # we prefer that one. + number = "#{_default_country_number}#{number.gsub(/^0/, '')}" end end # number = "#{_default_country_number}#{number}" unless Phony.plausible?(number) diff --git a/spec/lib/phony_rails_spec.rb b/spec/lib/phony_rails_spec.rb index 847b1ee..d5d1110 100644 --- a/spec/lib/phony_rails_spec.rb +++ b/spec/lib/phony_rails_spec.rb @@ -113,6 +113,8 @@ expect(PhonyRails.normalize_number('2318725305', country_code: 'US')).to eq('+12318725305') expect(PhonyRails.normalize_number('2318725305', default_country_code: 'US')).to eq('+12318725305') expect(PhonyRails.normalize_number('+2318725305', default_country_code: 'US')).to eq('+2318725305') + # expect(Phony.plausible?("#{PhonyRails.country_number_for('US')}02318725305")).to be_truthy + expect(PhonyRails.normalize_number('02318725305', default_country_code: 'US')).to eq('+12318725305') end it "should pass Github issue #89" do @@ -141,6 +143,15 @@ }).to raise_error(ArgumentError) end + it "should pass Github issue #95" do + number = '02031234567' + expect(number.phony_normalized(default_country_code: 'GB')).to eq('+442031234567') + end + + it 'should pass Github issue #121' do + number = '06-87-73-83-58' + expect(number.phony_normalized(default_country_code: 'FR')).to eq('+33687738358') + end end it "should not change original String" do