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 217 forks source link

AsYouType is not formatting for any country other than US #181

Closed imsam67 closed 6 years ago

imsam67 commented 6 years ago

I created a wrapper function in my React app that exposes all the functions in this library.

The function that handles as-you-type looks like this:

export const formatPhoneNumberAsYouType = (phoneNumber, countryId = null) => {

    if (typeof phoneNumber === "undefined" || phoneNumber === null || phoneNumber === "")
        return "";

    if (typeof countryId === "undefined" || countryId === null || countryId === "") {

        return new AsYouType().input(phoneNumber);
    } else {

        return new AsYouType(countryId.toUpperCase()).input(phoneNumber);
    }
}

For U.S. phone numbers this works perfectly fine and format them. As I step through the code, I see that it's executing fine but just not formatting numbers from other countries. I tried Germany and United Kingdom and all I get is the same numbers as I entered them without any formatting.

I also double-checked to see if I'm passing the correct country ID's. For Germany, I'm passing "de" and for United Kingdom, "gb". As you'll see in the code above, I'm also making sure that country codes are all upper case.

Any idea what may be the issue here?

catamphetamine commented 6 years ago

Strange. I did leave a comment but it disappeared.

If the above doesn't resolve the issue then add a test case in AsYouType.test.js illustrating the issue.