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

Parse international phone number without plus sign correctly #324

Closed hbouhadji closed 4 years ago

hbouhadji commented 5 years ago

This should fix the issue #316

With this example:

let result = parseNumber('33169454850', 'FR', { extended: true });
console.log(result);

what I get:

{
    country: 'FR',
    countryCallingCode: '33',
    carrierCode: undefined,
    valid: false,
    possible: false,
    phone: '33169454850',
    ext: undefined
}

what I should get:

{
    country: 'FR',
    countryCallingCode: '33',
    carrierCode: undefined,
    valid: true,
    possible: true,
    phone: '169454850',
    ext: undefined
}

https://libphonenumber.appspot.com/phonenumberparser?number=33169454850&country=FR

available for any questions

coveralls commented 5 years ago

Coverage Status

Coverage increased (+0.002%) to 99.831% when pulling 421658f26fe146f27d011cde226040b51fe910a0 on Hakim-Bou:master into 5aa41b74ae407557e3743bfee6723db31bc0e4d6 on catamphetamine:master.

mario-d-s commented 4 years ago

Hey @catamphetamine, I know you have been busy, but any chance you could still review + merge this? Your library has been great for us but it would be absolutely perfect if this was fixed!

catamphetamine commented 4 years ago

@mario-d-s Hi. I'm actually currently rewriting AsYouType formatter to fix the duplicate area code bug. As for this change, it looks clever. But should we include it in the library? I mean, it could very well be incorporated in developer's code as:

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)
    }
  }
}