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.76k stars 218 forks source link

Formatting possible but invalid numbers #424

Closed liuzach closed 2 years ago

liuzach commented 2 years ago

Hey there,

I recently upgraded from 1.2.15 to 1.9.52 and I noticed that this broke formatting of possible but invalid numbers (such as +11234567890). Previously I was using parseNumber with the extended: true option and passing that result into formatNumber with the 'National' format and that would format the above example number to (123) 456-7890.

After upgrading to the newest version that legacy API no longer seems to work -- it returns an empty string instead of the formatted number. Using the new API, I pass the phone number to parsePhoneNumber and call .format('NATIONAL') on the result of that. That works for valid numbers but for invalid numbers it returns the number unformatted (1234567890 for the example above).

Is there a way in the newest version to format these numbers that are possible but not valid?

catamphetamine commented 2 years ago

Using the new API, I pass the phone number to parsePhoneNumber and call .format('NATIONAL') on the result of that.

Yeah, that's the way. Or .formatNational().

That works for valid numbers but for invalid numbers it returns the number unformatted

That's also expected.

Is there a way in the newest version to format these numbers that are possible but not valid?

If it's not doing it by default then there's no way.