Skip to content

Commit

Permalink
Fix 46 wrong result local phone number (#51)
Browse files Browse the repository at this point in the history
* update metadata

* fix issue 46

* improve comment
  • Loading branch information
cedvdb authored Dec 7, 2023
1 parent 7ce2a07 commit a5672ce
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
14 changes: 8 additions & 6 deletions lib/src/parsers/phone_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ abstract class PhoneParser {
final callerMetadata = callerCountry != null
? MetadataFinder.findMetadataForIsoCode(callerCountry)
: null;

var destinationMetadata = destinationCountry != null
? MetadataFinder.findMetadataForIsoCode(destinationCountry)
: null;
Expand All @@ -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,
);
Expand Down Expand Up @@ -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
Expand All @@ -107,6 +108,7 @@ abstract class PhoneParser {

return metadata ??
callerMetadata ??
// default if nothing was found.
MetadataFinder.findMetadataForIsoCode(IsoCode.US);
}
}
32 changes: 16 additions & 16 deletions test/phone_number_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit a5672ce

Please sign in to comment.