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 d783c72..df7b42d 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) result = {} Phonelib.phone_data.each do |key, data| parsed = parse_single_country(phone, data) - if double_prefix_allowed?(data, phone, parsed && parsed[key]) + 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 26865a0..77454a4 100644 --- a/spec/phonelib_spec.rb +++ b/spec/phonelib_spec.rb @@ -1039,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')