catamphetamine / libphonenumber-js

A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript
https://catamphetamine.gitlab.io/libphonenumber-js/
MIT License
2.77k stars 218 forks source link

[BUG] findPhoneNumbersInText doesn't behave the same way with or without the countryCode #402

Closed kamalbennani closed 3 years ago

kamalbennani commented 3 years ago

First of all, I wanted to thank you so much for the amazing work you've accomplished and the time you are dedicating to maintain this library.

Steps to reproduce

We heavily use this library to find phone numbers in a given string/text and use findPhoneNumbersInText for that.

But we found a "strange" behavior, where if you run the following code:

const result = libphonenumber.findPhoneNumbersInText('0781161949, Rechercher. 0033781161949, Rechercher.', 'BE')
// You expect either the result to be an empty array or detect `0033781161949` as being an international FR number
// The output is '0033781161949' being detected as an international number.

But the issue is that when you don't provide a country, like this:

const result = libphonenumber.findPhoneNumbersInText('0781161949, Rechercher. 0033781161949, Rechercher.')
// The output is an empty array, which is actually a little bit "weird"? maybe there is an explanation to it.

Is it expected that we have this difference between putting an unrelated country to the number that this number is getting detected and not putting anything?

And the other stuff is that when you replace the "00" with a "+" it gets detected as an international French number without specifying the country code

And the other stuff also is that if you change 'BE' country with "US", it will return an empty array 🤷‍♂️

any help would be greatly appreciated

catamphetamine commented 3 years ago

It is expected for this library to not parse phone numbers not starting with a + when no default country is provided. The reason is that 0033781161949 number is still a French one, written in French notation. In US that would be something like 011 33781161949.

https://en.wikipedia.org/wiki/List_of_country_calling_codes#:~:text=For%20example%2C%20the%20international%20call,number%20with%20the%20plus%20sign.

kamalbennani commented 3 years ago

Thank you for your quick response.

I do agree with you, but what I don't understand, is why when we provide the 'BE' or even 'MA' countryCode as the default country, the number is still detected as a French number, but when providing another country such as 'US' or 'UK' the function returns an empty array. shouldn't return an empty array in both cases?

catamphetamine commented 3 years ago

I do agree with you, but what I don't understand, is why when we provide the 'BE' or even 'MA' countryCode as the default country, the number is still detected as a French number, but when providing another country such as 'US' or 'UK' the function returns an empty array. shouldn't return an empty array in both cases?

I assume that BE or MA also use the 00 prefix for international numbers while US and UK don't. You can check that in PhoneNumberMetadata.xml.