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

Handle omitted exit code correctly #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/src/number_parser/parsers/_international_prefix_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 5 additions & 3 deletions lib/src/number_parser/parsers/phone_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,22 @@ 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,
callerMetadata: callerMetadata,
);
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,
Expand Down