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.79k stars 216 forks source link

Weird behavior #316

Closed hbouhadji closed 4 years ago

hbouhadji commented 5 years ago

Hello I have a question,

I want to accept some phonenumbers like: 33612902554 (FR country)

If I try the google lib (https://rawgit.com/googlei18n/libphonenumber/master/javascript/i18n/phonenumbers/demo-compiled.html), the result is:

****Formatting Results:**** 
E164 format: +33612902554
Original format: 33 6 12 90 25 54
National format: 06 12 90 25 54
International format: +33 6 12 90 25 54
Out-of-country format from US: 011 33 6 12 90 25 54
Out-of-country format from Switzerland: 00 33 6 12 90 25 54

which is perfect for me, but with libphonenumber-js (https://catamphetamine.github.io/libphonenumber-js/) I have:

Number: +3333612902554
Country: FR
National: 33612902554
International: +33 33612902554
URItel: +3333612902554
Type—
Possible false
Valid false

which is not good for me (+3333 ...), I know it's because I didn't add the '+' but it's working with the google lib.

Do you have an explanation to this please ? thanks you

catamphetamine commented 5 years ago

Indeed, the library doesn't have this error correction feature. https://libphonenumber.appspot.com/phonenumberparser?number=33612902554+&country=FR I guess I won't be adding it currently though, but I'll leave this issue open just in case.

DaveLomber commented 5 years ago

Same issue, seems I can call an external phone number w/o leading '+' from my iPhone so it's more or less a correct format

hbouhadji commented 5 years ago

@DaveLomber There is a PR actually, i'm waiting @catamphetamine to review and merge it if it's correct for him

PR #324

DaveLomber commented 5 years ago

@Hakim-Bou oh this is great ! will wait for @catamphetamine feedback

catamphetamine commented 5 years ago

@Hakim-Bou Hello Hakim. Yeah, I've seen your PR. I'm currently having busy days and there're a couple of other penging bugs in libphonenumber-js so I'm not merging it yet until I have enough free time to read my own code and compare it to how Google does it in their library (and also maybe merge some of their commits during this/past year). But in future I guess this feature will be added because Google's library supports it.

hbouhadji commented 5 years ago

Any news @catamphetamine ?

catamphetamine commented 5 years ago

@Hakim-Bou It's funny but I still haven't looked into that because I was busy with other things. Perhaps in some future I'll look at it. But not now.

catamphetamine commented 4 years ago

@Hakim-Bou I finally found some time to close issues in this library. Your fix seems clever, but I don't feel like having such autocorrection feature in this library. I guess Google has their reasons to correct any errors, but I personally feel like some errors should stay non-corrected, like omitting + of an international number, or using a national prefix in AsYouType when inputting an international number.

Still, there could be a way to implement such feature without forking the library and modifying its code directly. Something like this:

import { parsePhoneNumberFromString, getCountryCallingCode } from 'libphonenumber-js'

export default function autoFixParseNumber(number, country) {
  const phoneNumber = parsePhoneNumberFromString(number, country)
  if (phoneNumber) {
    return phoneNumber
  }
  if (country && number[0] !== '+') {
    if (number.indexOf(getCountryCallingCode(country)) === 0) {
      number = number.slice(getCountryCallingCode(country).length)
      return parsePhoneNumberFromString(number, country)
    }
  }
}

Such code could be called then from an application. This way, the feature won't be in the library, but would still be present in the application.

catamphetamine commented 4 years ago

@Hakim-Bou Hello Hakim. I reviewed your feature request recently, and agree that it's actually reasonable, so I've implemented the leading + autocorrection feature in the new release libphonenumber-js@1.7.41. Thanks for the suggestion.

hbouhadji commented 4 years ago

@catamphetamine Hello Nikolay, That's a great news, thanks you, I'm gonna give an eye on it

hbouhadji commented 4 years ago

@catamphetamine that's seems good to me, except for the As you type method (https://catamphetamine.github.io/libphonenumber-js/)

catamphetamine commented 4 years ago

@Hakim-Bou

that's seems good to me, except for the As you type method

Good point, forgot about the .getNumber() method of AsYouType. Fixed in libphonenumber-js@1.7.42. To keep the code simpler, I didn't implement formatting of such numbers in AsYouType — it'll be a form of taxation of users that input incorrect numbers: such numbers would still be parsed, but they won't be formatted.

hbouhadji commented 4 years ago

@catamphetamine Perfect ! Thanks you 👍