3

I'm using libphonenumber with the npm package.

The problem I have is this one:

// Get an instance of `PhoneNumberUtil`. 
var phoneUtil = require('googlelibphonenumber').PhoneNumberUtil.getInstance();

phoneUtil.parse('+44') // throws exception
phoneUtil.parse('+44234') // gives me an object to work with

I need to be able to parse a number which can be complete or not. In the worst case scenario, it will be just the country code. And in this case, I want to be able to get the country for it.

Is there a way to accomplish it? The API is not very documented and I'm just experimenting with the REPL while looking at the sources.

pietro909
  • 1,811
  • 1
  • 19
  • 26

1 Answers1

7

You can't always find out the country given a phone code because, say, USA and Canada both have 1 code.

For incomplete numbers you can try using asYouType and then access the .country property as explained here:

https://github.com/halt-hammerzeit/libphonenumber-js

catamphetamine
  • 4,489
  • 30
  • 25
  • after trying a while, we decided to go for a different solution. Nonetheless, I think your answer is correct: I also was falling back on `asYouType` method for performing a _check_ – pietro909 Dec 06 '16 at 16:37