From dbc93513ce7a658259431e95219537801328a2e6 Mon Sep 17 00:00:00 2001 From: Matteo von Haxthausen Date: Mon, 29 Apr 2024 13:44:30 +0200 Subject: [PATCH] fix: phone number parser - handle omitted exit code correclty --- .../parsers/_international_prefix_parser.dart | 11 +++++++---- lib/src/number_parser/parsers/phone_parser.dart | 8 +++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/src/number_parser/parsers/_international_prefix_parser.dart b/lib/src/number_parser/parsers/_international_prefix_parser.dart index 9f4046f..8f7dd77 100644 --- a/lib/src/number_parser/parsers/_international_prefix_parser.dart +++ b/lib/src/number_parser/parsers/_international_prefix_parser.dart @@ -5,22 +5,25 @@ abstract class InternationalPrefixParser { /// /// It expects a normalized [phoneNumber]. /// if phone starts with + it is removed - static String removeExitCode( + static (String, bool) removeExitCode( String phoneNumber, { PhoneMetadata? callerCountryMetadata, PhoneMetadata? destinationCountryMetadata, }) { if (phoneNumber.startsWith('+')) { - return phoneNumber.substring(1); + return (phoneNumber.substring(1), true); } // if the caller country was provided it's easy, just remove the exit code // from the phone number if (callerCountryMetadata != null) { - return _removeExitCodeWithMetadata(phoneNumber, callerCountryMetadata); + return ( + _removeExitCodeWithMetadata(phoneNumber, callerCountryMetadata), + true + ); } - return phoneNumber; + return (phoneNumber, false); } static String _removeExitCodeWithMetadata( diff --git a/lib/src/number_parser/parsers/phone_parser.dart b/lib/src/number_parser/parsers/phone_parser.dart index 4d3339a..99a4ab3 100644 --- a/lib/src/number_parser/parsers/phone_parser.dart +++ b/lib/src/number_parser/parsers/phone_parser.dart @@ -45,12 +45,12 @@ abstract class PhoneParser { ? MetadataFinder.getMetadataForIsoCode(destinationCountry) : null; - final withoutExitCode = InternationalPrefixParser.removeExitCode( + final (withoutExitCode, containsExitCode) = + InternationalPrefixParser.removeExitCode( phoneNumber, destinationCountryMetadata: destinationMetadata, callerCountryMetadata: callerMetadata, ); - final containsExitCode = withoutExitCode.length != phoneNumber.length; // if no destination metadata was provided we have to find it, destinationMetadata ??= _findDestinationMetadata( phoneWithoutExitCode: withoutExitCode, @@ -58,7 +58,9 @@ abstract class PhoneParser { ); var national = withoutExitCode; // if there was no exit code then we assume we are dealing with a national number - if (containsExitCode) { + // if the number starts with the country code, we remove the country code + if (containsExitCode || + withoutExitCode.startsWith(destinationMetadata.countryCode)) { national = CountryCodeParser.removeCountryCode( withoutExitCode, destinationMetadata.countryCode,