Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing phone number incorrectly when area code matches country code #79

Open
conorhynesire opened this issue Jun 22, 2016 · 0 comments

Comments

@conorhynesire
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant