From a5672ce783d29c0ee95e27bed4bf01e4012371e8 Mon Sep 17 00:00:00 2001 From: cedvdb Date: Thu, 7 Dec 2023 14:25:22 +0100 Subject: [PATCH] Fix 46 wrong result local phone number (#51) * update metadata * fix issue 46 * improve comment --- lib/src/parsers/phone_parser.dart | 14 ++++++++------ test/phone_number_test.dart | 32 +++++++++++++++---------------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/src/parsers/phone_parser.dart b/lib/src/parsers/phone_parser.dart index 2aaf8f8..0fd50af 100644 --- a/lib/src/parsers/phone_parser.dart +++ b/lib/src/parsers/phone_parser.dart @@ -39,6 +39,7 @@ abstract class PhoneParser { final callerMetadata = callerCountry != null ? MetadataFinder.findMetadataForIsoCode(callerCountry) : null; + var destinationMetadata = destinationCountry != null ? MetadataFinder.findMetadataForIsoCode(destinationCountry) : null; @@ -49,8 +50,10 @@ abstract class PhoneParser { destinationCountryMetadata: destinationMetadata, callerCountryMetadata: callerMetadata, ); + // if no destination metadata was provided we have to find it, destinationMetadata ??= _findDestinationMetadata( + exitCode: exitCode, phoneWithoutExitCode: withoutExitCode, callerMetadata: callerMetadata, ); @@ -87,15 +90,13 @@ abstract class PhoneParser { // find destination of a normalized phone number, which supposedly // starts with a country code. static PhoneMetadata _findDestinationMetadata({ + required String exitCode, required String phoneWithoutExitCode, required PhoneMetadata? callerMetadata, }) { - final callerNationalPrefix = callerMetadata?.nationalPrefix; - // if it starts with the national prefix of the caller then we can safely - // assume that the caller calls in the same country - if (callerMetadata != null && - callerNationalPrefix != null && - (phoneWithoutExitCode.startsWith(callerNationalPrefix))) { + // if there was not an exit code then we can use the caller metadata + // since we did not exit + if (exitCode.isEmpty && callerMetadata != null) { return callerMetadata; } // if no caller was provided we need to make a best guess given the country code @@ -107,6 +108,7 @@ abstract class PhoneParser { return metadata ?? callerMetadata ?? + // default if nothing was found. MetadataFinder.findMetadataForIsoCode(IsoCode.US); } } diff --git a/test/phone_number_test.dart b/test/phone_number_test.dart index 937efbc..2531119 100644 --- a/test/phone_number_test.dart +++ b/test/phone_number_test.dart @@ -98,22 +98,22 @@ void main() { equals('+5493435551212')); }); - // test( - // 'should parse local numbers w/o national prefix as they belong to callerCountry', - // () { - // expect( - // PhoneNumber.parse('(888) 555-5512', callerCountry: IsoCode.US) - // .international, - // equals('+18885555512')); - // expect( - // PhoneNumber.parse('(555) 522-8243', callerCountry: IsoCode.US) - // .international, - // equals('+15555228243')); - // expect( - // PhoneNumber.parse('(707) 555-1854', callerCountry: IsoCode.US) - // .international, - // equals('+17075551854')); - // }); + test( + 'should parse local numbers w/o national prefix as they belong to caller country', + () { + expect( + PhoneNumber.parse('(888) 555-5512', callerCountry: IsoCode.US) + .international, + equals('+18885555512')); + expect( + PhoneNumber.parse('(555) 522-8243', callerCountry: IsoCode.US) + .international, + equals('+15555228243')); + expect( + PhoneNumber.parse('(707) 555-1854', callerCountry: IsoCode.US) + .international, + equals('+17075551854')); + }); test( 'should output a national number in its international version only when valid',