phonie icon indicating copy to clipboard operation
phonie copied to clipboard

Parsing phone number incorrectly when area code matches country code

Open conorhynesire opened this issue 9 years ago • 0 comments

We were running into an issue with an Italian number that had an area code that was matching the country code.

This is an example of the problem phone no: +39 393 2612345

We would pass this into phonie and get back an incorrect phone number: Phonie::Phone.parse("3932612345", country_code: "39").format("00%c%a%n") => "003932612345"

Phonie as it stands does a full number regex pattern match when it is attempting to parse, which checks for country code, area code and the number.

Full Number Match Regexp.new("^[+]?(#{country_code})(#{area_code})(#{local_number_format})$")

The issue is 393 is the area code which contains the same numbers as the country code of 39. 326 is also a valid area code, phonie thinks this number is alright since it's still valid regex.

What I had proposed on doing was rewriting the parse function of parser class to read the area code first if the country code was present.

An issue with this became present in the cn_test.rb spec however as the country code was both present in the phone number and within the country_code variable.

https://github.com/wmoxam/phonie/blob/master/lib/phonie/parser.rb#L39 What I wanted to do:

def parse(number, default_area_code = nil)
  parsed_number = parse_area_code_match(number) if country_code.present?
  parsed_number ||= parse_full_match(number)
  parsed_number ||= parse_with_default(number, default_area_code)
  parsed_number ||= {}
end

Any help on this would be appreciated!

conorhynesire avatar Jun 22 '16 13:06 conorhynesire