-
Notifications
You must be signed in to change notification settings - Fork 135
Description
Hello. I appreciate the contributors' work on Phonelib.
I'm updating the gem to 0.10.5 and I've noticed a behavioral difference.
OS: Mac Sequoia, Ruby: 3.3.6, Phonelib: 0.10.2 and 0.10.3
Example:
The number("+813000000000") matches Core::GENERAL
but does not have a valid type.
https://libphonenumber.appspot.com/phonenumberparser?number=%2B813000000000
# v0.10.3~
> Phonelib.parse("+813000000000").country
=> nil
> Phonelib.parse("+813000000000", "JP").country
=> "JP"
# ~v0.10.2
> Phonelib.parse("+813000000000").country
=> "JP"
> Phonelib.parse("+813000000000", "JP").country
=> "JP"
Q. Which behavior is correct for this gem?
It seems this change originates from v0.10.3:
phonelib/lib/phonelib/phone_analyzer.rb
Line 141 in 894441b
parsed = parse_single_country(with_replaced_national_prefix(phone, data), data) unless parsed && parsed[key] && parsed[key][:valid].size > 0 |
The parse_single_country
method expects an E.164 format, but the with_replaced_national_prefix
method removes the international dialing code, causing it to not match.
Within the with_replaced_national_prefix
, if phone is a match with data[Core::NATIONAL_PREFIX_FOR_PARSING]
, the country code 81 is added back. But, var phone
like "8130..." becomes "30...", so it does not match the following regexp:
# data[Core::NATIONAL_PREFIX_FOR_PARSING]
/^(?:(000[259]\d{6})$|(?:(?:003768)0?)|0)/
This seems like a significant change for a minor update, so I thought it might be unintentional.
Additional:
For US numbers, it seems that the phone number transformation is not performed due to the NATIONAL_PREFIX_TRANSFORM_RULE
being nil, resulting in the same outcome line 140 and 141.
phonelib/lib/phonelib/phone_analyzer.rb
Lines 140 to 141 in 894441b
parsed = parse_single_country(phone, data) | |
parsed = parse_single_country(with_replaced_national_prefix(phone, data), data) unless parsed && parsed[key] && parsed[key][:valid].size > 0 |
So, it is possible to get the country even with an invalid number.
# ~v0.10.2 and v0.10.3~ same
Phonelib.parse("+10000000000").country
=> "US"
Phonelib.parse("+10000000000").instance_variable_get("@data")["US"][:valid]
=> []
I look forward to your reply.