daddyz / phonelib

Ruby gem for phone validation and formatting using google libphonenumber library data
MIT License
1.08k stars 132 forks source link

Improving formatting for possible (but not valid) numbers? #284

Closed alexeyr-ci closed 1 year ago

alexeyr-ci commented 1 year ago

E.g. +15555555555 is possible but not valid. So I get

[1] pry(main)> phone = Phonelib.parse("+15555555555")
...
[2] pry(main)> phone.international
"+15555555555"                    
[3] pry(main)> phone.national
"5555555555" 

Still it's clear it should be formatted as (555) 555-55-55 nationally, and +1 555-555-55-55 internationally, and indeed if I do Phonelib.add_additional_regex :us, Phonelib::Core::MOBILE, '555\d{7}', Phonelib knows how to format it.

Indeed, https://libphonenumber.appspot.com/phonenumberparser?number=%2B15555555555 gives

Original format +1 555-555-5555
National format (555) 555-5555
International format    invalid

but from its code I think invalid is due to an explicit check and "Original format" is the international format in this case.

Is it possible to change the formatting methods to work in this way or at least to add an option?

daddyz commented 1 year ago

@alexeyr-ci changed behaviour in v0.8.4 to provide formatted result when number is possible instead of valid

tilo commented 9 months ago

@daddyz a version change from 0.8.3 to 0.9.0 might have been better here, well actually from 0.9.x to 0.10.0 because of the other breaking change earlier https://github.com/daddyz/phonelib/issues/297#issue-2135368672