-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update metadata * revert dependency validator * refactor * use record
- Loading branch information
Showing
19 changed files
with
255 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,34 @@ | ||
import 'dart:math'; | ||
|
||
import 'package:phone_numbers_parser/phone_numbers_parser.dart'; | ||
import 'package:phone_numbers_parser/src/constants/constants.dart'; | ||
import 'package:phone_numbers_parser/src/models/phone_number_exceptions.dart'; | ||
import 'package:phone_numbers_parser/src/metadata/metadata_finder.dart'; | ||
|
||
import '_text_parser.dart'; | ||
|
||
abstract class CountryCodeParser { | ||
/// normalize a country calling code to return only digits | ||
static String normalizeCountryCode(String countryCode) { | ||
countryCode = TextParser.normalize(countryCode); | ||
|
||
if (countryCode.startsWith('+')) { | ||
countryCode = countryCode.replaceFirst('+', ''); | ||
} | ||
// country code don't start with zero | ||
if (countryCode.startsWith('0')) { | ||
throw PhoneNumberException( | ||
code: Code.invalidCountryCallingCode, | ||
description: | ||
'country calling code do not start with 0, was $countryCode'); | ||
} | ||
if (int.tryParse(countryCode) == null) { | ||
throw PhoneNumberException( | ||
code: Code.invalidCountryCallingCode, | ||
description: 'country calling code must be digits, was $countryCode. ' | ||
'Maybe you wanted to parseWithIsoCode ?'); | ||
} | ||
if (countryCode.length < Constants.minLengthCountryCallingCode || | ||
countryCode.length > Constants.maxLengthCountryCallingCode) { | ||
throw PhoneNumberException( | ||
code: Code.invalidCountryCallingCode, | ||
description: | ||
'country calling code has an invalid length, was $countryCode'); | ||
} | ||
return countryCode; | ||
} | ||
|
||
/// tries to find a country calling code at the start of a phone number | ||
static String extractCountryCode(String phoneNumber) { | ||
final maxLength = | ||
min(phoneNumber.length, Constants.maxLengthCountryCallingCode); | ||
var potentialCountryCode = phoneNumber.substring(0, maxLength); | ||
potentialCountryCode = normalizeCountryCode(potentialCountryCode); | ||
static (String countryCode, String nsn) extractCountryCode( | ||
String phoneNumber, | ||
) { | ||
final maxCountryCodeLength = min( | ||
phoneNumber.length, | ||
Constants.maxLengthCountryCallingCode, | ||
); | ||
var potentialCountryCode = phoneNumber.substring(0, maxCountryCodeLength); | ||
|
||
for (var i = 1; i <= potentialCountryCode.length; i++) { | ||
try { | ||
final potentialCountryCodeFit = potentialCountryCode.substring(0, i); | ||
MetadataFinder.getMetadatasForCountryCode(potentialCountryCodeFit); | ||
return potentialCountryCodeFit; | ||
// ignore: empty_catches | ||
} catch (e) {} | ||
final potentialCountryCodeFit = potentialCountryCode.substring(0, i); | ||
final nsn = phoneNumber.substring(i); | ||
final countryMetadata = MetadataFinder.findMetadataForCountryCode( | ||
potentialCountryCodeFit, | ||
nsn, | ||
); | ||
if (countryMetadata != null) { | ||
return (countryMetadata.countryCode, nsn); | ||
} | ||
} | ||
throw PhoneNumberException( | ||
code: Code.notFound, | ||
description: | ||
'country calling code not found in phone number $phoneNumber'); | ||
} | ||
|
||
// removes the country code at the start of a phone number | ||
static String removeCountryCode(String phoneNumber, String countryCode) { | ||
if (phoneNumber.startsWith(countryCode)) { | ||
return phoneNumber.substring(countryCode.length); | ||
} | ||
return phoneNumber; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.