cedvdb / phone_numbers_parser

Dart phone number parser, based on libphonenumber and PhoneNumberKit.
https://pub.dev/packages/phone_numbers_parser
MIT License
63 stars 33 forks source link

Using this library to format phone number #1

Closed orestesgaolin closed 3 years ago

orestesgaolin commented 3 years ago

I wonder if it is possible to use this library to reverse its purpose i.e. to format a set of numbers into a phone number for a. given country:

1234567890 -> (US) -> 123-456-7890

cedvdb commented 3 years ago

It's definitely doable. Someone else asked for this feature this morning. So this confirms it's a feature that will be used. I'm going to push the metadata required to do that this weekend.

cedvdb commented 3 years ago

The code used for formatting in PhoneNumberKit is there https://github.com/marmelroy/PhoneNumberKit/blob/master/PhoneNumberKit/Formatter.swift , the main method being formatNationalNumber as extensions are not supported by this lib.

The code used by libphonenumber is here https://github.com/google/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java

If anyone is interested in making a PR that would be welcome. I've added the necessary metadata to the latest version of dart_phone_metadata.

bsutton commented 3 years ago

Allowing formatting into local and national no.s would also be useful:

e.g.

var phone = PhoneParser.parseRaw('+61383208100');
phone.toLocal == '0383208100';
phone.toNational = '61383208100';
cedvdb commented 3 years ago

If someone wants to have a go at this, I've separated the metadatas in multiple maps. PhoneMetadataFormats is the one that is required for this. It is expected that a new PhoneNumberFormatter class would be created to not increase the size of the library if one only needs the parsing and not the formatting, as it is hard to know in advance what is tree shaken and what is not.

Some inspiration here :

https://github.com/google/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java

var phone = PhoneParser.parseRaw('+61383208100');
phone.toLocal == '0383208100';
phone.toInternational = '61383208100';

Note that when phone_numbers_parser parses a phone number it transforms it from its local version to international version. Some local phone numbers in Argentina are very different from their international version from what I understand. Example: in argentina 0343 15 555 1212 (local) is exactly the same number as +54 9 343 555 1212 (international).

So it's not just a matter of adding / removing the nationalPrefix.

When the parser is used to return a phone number, it does that transformation but does not save the original, which could be an issue.

cedvdb commented 3 years ago

Formatting available in v3.0.0. If more formats are needed, please submit a PR.