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',