formatInOriginalFormat method
Formats a phone number using the original phone number format that the number
is parsed from. The original format is embedded in the countryCodeSource
field of the PhoneNumber object passed in. If such information is missing,
the number will be formatted into the national
format by default. When the
number contains a leading zero and this is unexpected for this country, or
we don't have a formatting pattern for the number, the method returns the
raw input when it is available.
Note this method guarantees no digit will be inserted, removed or modified as a result of formatting.
number
the phone number that needs to be formatted in its original number format.
regionCallingFrom
the region whose IDD needs to be prefixed if the original number has one.
Implementation
String formatInOriginalFormat(PhoneNumber number, String regionCallingFrom) {
if (number.hasRawInput() && !_hasFormattingPatternForNumber(number)) {
// We check if we have the formatting pattern because without that, we might
// format the number as a group without national prefix.
return number.rawInput;
}
if (!number.hasCountryCodeSource()) {
return format(number, PhoneNumberFormat.national);
}
String formattedNumber;
switch (number.countryCodeSource) {
case PhoneNumber_CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN:
formattedNumber = format(number, PhoneNumberFormat.international);
break;
case PhoneNumber_CountryCodeSource.FROM_NUMBER_WITH_IDD:
formattedNumber =
formatOutOfCountryCallingNumber(number, regionCallingFrom);
break;
case PhoneNumber_CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN:
formattedNumber =
format(number, PhoneNumberFormat.international).substring(1);
break;
case PhoneNumber_CountryCodeSource.FROM_DEFAULT_COUNTRY:
// Fall-through to default case.
default:
String regionCode = getRegionCodeForCountryCode(number.countryCode);
// We strip non-digits from the NDD here, and from the raw input later,
// so that we can compare them easily.
String? nationalPrefix = getNddPrefixForRegion(regionCode, true);
String nationalFormat = format(number, PhoneNumberFormat.national);
if (nationalPrefix == null || nationalPrefix.isEmpty) {
// If the region doesn't have a national prefix at all, we can safely
// return the national format without worrying about a national prefix
// being added.
formattedNumber = nationalFormat;
break;
}
// Otherwise, we check if the original number was entered with a national
// prefix.
if (_rawInputContainsNationalPrefix(
number.rawInput, nationalPrefix, regionCode)) {
// If so, we can safely return the national format.
formattedNumber = nationalFormat;
break;
}
// Metadata cannot be null here because getNddPrefixForRegion() (above)
// returns null if there is no metadata for the region.
PhoneMetadata metadata = getMetadataForRegion(regionCode: regionCode)!;
String nationalNumber = getNationalSignificantNumber(number);
NumberFormat? formatRule = chooseFormattingPatternForNumber(
metadata.numberFormat, nationalNumber);
// The format rule could still be null here if the national number was 0
// and there was no raw input (this should not be possible for numbers
// generated by the phonenumber library as they would also not have a
// country calling code and we would have exited earlier).
if (formatRule == null) {
formattedNumber = nationalFormat;
break;
}
// When the format we apply to this number doesn't contain national
// prefix, we can just return the national format.
// TODO: Refactor the code below with the code in
// isNationalPrefixPresentIfRequired.
String candidateNationalPrefixRule =
formatRule.nationalPrefixFormattingRule;
// We assume that the first-group symbol will never be _before_ the
// national prefix.
int indexOfFirstGroup = candidateNationalPrefixRule.indexOf('\$1');
if (indexOfFirstGroup <= 0) {
formattedNumber = nationalFormat;
break;
}
candidateNationalPrefixRule =
candidateNationalPrefixRule.substring(0, indexOfFirstGroup);
candidateNationalPrefixRule =
normalizeDigitsOnly(candidateNationalPrefixRule);
if (candidateNationalPrefixRule.isEmpty) {
// National prefix not used when formatting this number.
formattedNumber = nationalFormat;
break;
}
// Otherwise, we need to remove the national prefix from our output.
NumberFormat numFormatCopy = formatRule.deepCopy();
numFormatCopy.clearNationalPrefixFormattingRule();
formattedNumber = formatByPattern(
number, PhoneNumberFormat.national, [numFormatCopy]);
break;
}
String rawInput = number.rawInput;
// If no digit is inserted/removed/modified as a result of our formatting, we
// return the formatted phone number; otherwise we return the raw input the
// user entered.
if (formattedNumber.isNotEmpty && rawInput.isNotEmpty) {
String normalizedFormattedNumber =
normalizeDiallableCharsOnly(formattedNumber);
String normalizedRawInput = normalizeDiallableCharsOnly(rawInput);
if (normalizedFormattedNumber != normalizedRawInput) {
formattedNumber = rawInput;
}
}
return formattedNumber;
}