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

Fix nsn starting with national prefix #40

Closed Alex-Usmanov closed 11 months ago

Alex-Usmanov commented 1 year ago

When removing a national prefix in national number parser, sometimes the removal does not occur (and further down the line, when transform rule is applied, weird behavior is encountered).

In order to remedy this (and allow for type-as-you-go formatting), a measure is necessary to remove the previously applied transform rule (leadingDigits from metadata do not reflect transform rule all the time - example isocode=cc). So, I added a mechanism for removal of this transform rule (which is usually the national prefix).

Alex-Usmanov commented 1 year ago

Hi! Sorry for late response.

Turns out the issue was slightly different that I anticipated (moreover, some tests were failing in my version of solution). So, the issue is: when formatting a national number, a transformRule might be applied when it is unnecessary.

For instance we shall take this number: 4416666 When parsing it with destinationCountry=IsoCode.BM, the transform rule is applied and 4416666 turns into 4414416666. (notice how 441 was applied twice via transformRule)

To fix this, I added a check to the transform function to see if the number is already transformed and do not make the transformation again.

Added a test case: phone_number_test.dart:190

cedvdb commented 11 months ago

Sorry for the late reply from myself too.

At first glance I've the following worries:

  1. It seems hacky to parse the rule pattern like that
  2. I imagine the libphonenumber library has the same issue and must be doing something in order to avoid this.
  3. There are nationalPrefixFormattingRule of the sort r"$NP$FG" which I'm not sure how that'd interact with the given code.

Would this alternative solution of only apply ing the transform rule when the phone number is valid work ?

cedvdb commented 11 months ago

I'm taking a look once again at this to fix it, but I do not understand the actual issue. Could you create an issue with the steps to reproduce your issue ? Please go in detail so there is no misunderstanding. I'm going to close this until then and will reopen if the issue is created.