libphonenumber [Js] get country code only
Asked Answered
H

1

6

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.

Hocuspocus answered 30/11, 2016 at 16:18 Comment(0)
T
9

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

Tolu answered 2/12, 2016 at 8:49 Comment(1)
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 checkHocuspocus

© 2022 - 2024 — McMap. All rights reserved.