Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot accurately parse a phone number with country code and no exit code (or +) #39

Closed
josh-burton opened this issue Feb 24, 2023 · 3 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@josh-burton
Copy link

I was previously using this library to sanitize phone numbers in a variety of formats.

This test used to parse (input -> sanitised output)

640211234567 -> +64211234567

Now it seems this package no longer interprets the country code, because there is no leading exit code (or +).

When I parse this number, the international number returned is +64640211234567 (double 64)

@cedvdb
Copy link
Owner

cedvdb commented Feb 24, 2023

Could you give a few code examples ? Please be exhaustive as there are some use cases that might be conflicting. In particular do you specify a caller or destination country. Do you parse incomplete phone numbers ?

@josh-burton
Copy link
Author

Here's my test cases that were all passing with the 5.x version of the library:

void main() {
  group('phone number test', () {
    parse(String input, {required IsoCode countryCode}) {
      return PhoneNumber.parse(
        input,
        callerCountry: countryCode,
        destinationCountry: countryCode,
      );
    }

    sanitizePhoneNumber(String number, {required IsoCode countryCode}) {
      return parse(number, countryCode: countryCode).international;
    }

    format(String input, {required IsoCode countryCode}) {
      final phoneNumber = parse(input, countryCode: countryCode);
      final formattedNsn = phoneNumber.getFormattedNsn();
      return formattedNsn;
    }

    test('sanitize', () async {
      var expected = '+64211234567';
      expect(sanitizePhoneNumber('0211234567', countryCode: IsoCode.NZ), expected);
      expect(sanitizePhoneNumber('021 123 4567', countryCode: IsoCode.NZ), expected);
      expect(sanitizePhoneNumber('211234567', countryCode: IsoCode.NZ), expected);
      expect(sanitizePhoneNumber('21 1234 567', countryCode: IsoCode.NZ), expected);
      expect(sanitizePhoneNumber('640211234567', countryCode: IsoCode.NZ), expected);
      expect(sanitizePhoneNumber('64211234567', countryCode: IsoCode.NZ), expected);
      expect(sanitizePhoneNumber('+640211234567', countryCode: IsoCode.NZ), expected);
      expect(sanitizePhoneNumber('+64211234567', countryCode: IsoCode.NZ), expected);
      expect(sanitizePhoneNumber('+211234567', countryCode: IsoCode.NZ), expected);
      expect(sanitizePhoneNumber('+21 123 4567', countryCode: IsoCode.NZ), expected);
    });

    test('phone formatter test', () async {
      expect(format("211234567", countryCode: IsoCode.NZ), equals("21 123 4567"));
      expect(format("0211234567", countryCode: IsoCode.NZ), equals("21 123 4567"));
      expect(format("64211234567", countryCode: IsoCode.NZ), equals("21 123 4567"));
      expect(format("+64211234567", countryCode: IsoCode.NZ), equals("21 123 4567"));
    });
  });
}

Hopefully that helps :) thanks for the prompt response.

@cedvdb cedvdb added good first issue Good for newcomers help wanted Extra attention is needed and removed good first issue Good for newcomers labels Nov 27, 2023
@cedvdb
Copy link
Owner

cedvdb commented Dec 7, 2023

Fixed in #53

Except this one : +21 123 4567' with IsoCode.NZ

As it does not make sense... +211 is for another country than NZ

@cedvdb cedvdb closed this as completed Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants