diff --git a/README.md b/README.md index 39dd158..5778a02 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,12 @@ To disable sanitizing of passed phone number (keeping digits only) Phonelib.strict_check = true ``` +To disable sanitizing of double prefix on passed phone number + +```ruby +Phonelib.strict_double_prefix_check = true +``` + To set different extension separator on formatting, this setting doesn't affect parsing. Default setting is ';' ``` ruby diff --git a/lib/phonelib/core.rb b/lib/phonelib/core.rb index 82a4c78..9e4b312 100644 --- a/lib/phonelib/core.rb +++ b/lib/phonelib/core.rb @@ -107,6 +107,22 @@ def strict_check=(strict) @@strict_check = strict end + # @private strict double prefix check for validator, doesn't sanitize number + @@strict_double_prefix_check = false + + # getter for strict double prefix check flag + # @return [Boolean] Flag defines whether to do strict double prefix parsing check + def strict_double_prefix_check + @@strict_double_prefix_check + end + + # setter for strict double prefix check flag + # @param strict [Boolean] make a strict double prefix parsing or not + # @return [Boolean] Flag defines whether to do strict double prefix parsing check + def strict_double_prefix_check=(strict) + @@strict_double_prefix_check = strict + end + @@override_phone_data = nil # setter for data file to use def override_phone_data=(file_path) diff --git a/lib/phonelib/phone_analyzer.rb b/lib/phonelib/phone_analyzer.rb index 0655758..3a80809 100644 --- a/lib/phonelib/phone_analyzer.rb +++ b/lib/phonelib/phone_analyzer.rb @@ -24,7 +24,7 @@ def analyze(phone, passed_country) # all is good, return result when passed_country.nil? # trying for all countries if no country was passed - detect_and_parse phone + detect_and_parse(phone, country) when country_can_dp?(country) # if country allows double prefix trying modified phone parse_country(changed_dp_phone(country, phone), country) @@ -102,11 +102,11 @@ def parse_single_country(e164, data) # ==== Attributes # # * +phone+ - phone number for parsing - def detect_and_parse(phone) + def detect_and_parse(phone, country = nil) result = {} Phonelib.phone_data.each do |key, data| parsed = parse_single_country(phone, data) - if double_prefix_allowed?(data, phone, parsed && parsed[key]) && key == Phonelib.default_country.to_s.upcase + if (!Phonelib.strict_double_prefix_check || key == country) && double_prefix_allowed?(data, phone, parsed && parsed[key]) parsed = parse_single_country(changed_dp_phone(key, phone), data) end result.merge!(parsed) unless parsed.nil? diff --git a/spec/phonelib_spec.rb b/spec/phonelib_spec.rb index 6d73883..77454a4 100644 --- a/spec/phonelib_spec.rb +++ b/spec/phonelib_spec.rb @@ -643,7 +643,6 @@ context 'issue #61' do it 'should be valid number in India' do - Phonelib.default_country = "IN" phone = Phonelib.parse('9111844757') expect(phone.valid?).to be true expect(phone.sanitized).to eq('9111844757') @@ -659,20 +658,6 @@ expect(phone.sanitized).to eq('4949266444201') expect(phone.e164).to eq('+4949266444201') end - - it 'should be invalid number outside India' do - Phonelib.default_country = nil - phone = Phonelib.parse('9111844757') - expect(phone.valid?).to be false - expect(Phonelib.valid?('919111844757')).to be true - - phone = Phonelib.parse('49266444201') - expect(phone.valid?).to be true - phone = Phonelib.parse('4949266444201') - expect(phone.valid?).to be true - expect(phone.sanitized).to eq('4949266444201') - expect(phone.e164).to eq('+4949266444201') - end end context 'issue #60' do @@ -930,7 +915,6 @@ context 'issue #105' do it 'should be valid when original without +' do - Phonelib.default_country = :IN expect(Phonelib.valid?('9183082081')).to be true expect(Phonelib.valid_for_country?('9183082081', 'IN')).to be true end @@ -1055,6 +1039,54 @@ end end + context 'issue #161' do + before do + Phonelib.strict_double_prefix_check = false + end + + context 'when strict_double_prefix_check is false' do + it 'should be valid number outside the country' do + Phonelib.default_country = nil + phone = Phonelib.parse('9111844757') + expect(phone.valid?).to be true + expect(Phonelib.valid?('919111844757')).to be true + end + + it 'should be valid number inside the country' do + phone = Phonelib.parse('9111844757', 'IN') + expect(phone.valid?).to be true + expect(Phonelib.valid?('919111844757')).to be true + + Phonelib.default_country = 'IN' + phone = Phonelib.parse('9111844757') + expect(phone.valid?).to be true + end + end + + context 'when strict_double_prefix_check is true' do + before do + Phonelib.strict_double_prefix_check = true + end + + it 'should be invalid number outside the country' do + Phonelib.default_country = nil + phone = Phonelib.parse('9111844757') + expect(phone.valid?).to be false + expect(Phonelib.valid?('919111844757')).to be true + end + + it 'should be valid number inside the country' do + phone = Phonelib.parse('9111844757', 'IN') + expect(phone.valid?).to be true + expect(Phonelib.valid?('919111844757')).to be true + + Phonelib.default_country = 'IN' + phone = Phonelib.parse('9111844757') + expect(phone.valid?).to be true + end + end + end + context 'valid_country_name method' do it 'should not return name for invalid number' do phone = Phonelib.parse('+12121231234')