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

Problem with Valid SG mobile numbers returning No Type (as of 1.6.0) #288

Closed dortamur closed 5 years ago

dortamur commented 5 years ago

I have a case where a given number is returning no Type, whereas the same number in Google's library (and other online checkers) correctly returns type "MOBILE". This case works in libphonenumber-js versions prior to 1.6.0 (using older API - tested with 1.5.2), but anything later than 1.6.0 inclusive fails.

The number is +6584655555 (last 5 digits changed for privacy). I use Default Country "AU" (as that's where I am, and most of our user). The library correctly identifies it as a Valid number from country "SG" - but the type is undefined.

Google Test: https://libphonenumber.appspot.com/phonenumberparser?number=%2B6584655555&country=AU

I have tried using "metadata.full.json" - although normally I use "metadata.mobile.json".

Minimal script:

const PhoneUtil = require('libphonenumber-js')
const metadata = require('libphonenumber-js/metadata.full.json')

let number = PhoneUtil.parsePhoneNumberFromString('+6584685555', 'AU', metadata)
let type = number.getType(number)
console.log({number, type})
catamphetamine commented 5 years ago

Works for me:

const phoneNumber2 = parsePhoneNumberFromString('+6584655555')
phoneNumber2.getType().should.equal('MOBILE')

You can create a pull request with a test which you think illustrates your issue. See tests/parsePhoneNumberFromString.test.js.

catamphetamine commented 5 years ago

@Dortamur Oh, I get it now: you're importing from the wrong package. See readme on min vs max vs mobile.

dortamur commented 5 years ago

Oh right! I get it now! I misinterpreted the metadata docs - since I was used to the older API, I thought metadata still needed to be imported separately - but instead, each metadata variant replaces the core library.

So my code should instead be:

const PhoneUtil = require('libphonenumber-js/mobile')

let number = PhoneUtil.parsePhoneNumberFromString('+6584685555', 'AU')
let type = number.getType(number)
console.log({number, type})

...which does return type == 'MOBILE'

Might I suggest adding a simple example code to the min/max/mobile section to illustrate the different metadata in action.

Thanks!

catamphetamine commented 5 years ago

@Dortamur Yeah, I guess that would make sense.. Added two examples to .getType() and .isValid() sections of the README.

dortamur commented 5 years ago

Great! Thanks.