formatByPattern method

String formatByPattern(
  1. PhoneNumber number,
  2. PhoneNumberFormat numberFormat,
  3. List<NumberFormat> userDefinedFormats
)

Formats a phone number in the specified format using client-defined formatting rules. Note that if the phone number has a country calling code of zero or an otherwise invalid country calling code, we cannot work out things like whether there should be a national prefix applied, or how to format extensions, so we return the national significant number with no formatting applied.

number the phone number to be formatted. numberFormat the format the phone number should be formatted into. userDefinedFormats formatting rules specified by clients.

Implementation

String formatByPattern(PhoneNumber number, PhoneNumberFormat numberFormat,
    List<NumberFormat> userDefinedFormats) {
  int countryCallingCode = number.countryCode;

  String nationalSignificantNumber = getNationalSignificantNumber(number);
  if (!_hasValidCountryCallingCode(countryCallingCode)) {
    return nationalSignificantNumber;
  }
  // Note getRegionCodeForCountryCode() is used because formatting information
  // for regions which share a country calling code is contained by only one
  // region for performance reasons. For example, for NANPA regions it will be
  // contained in the metadata for US.
  String regionCode = getRegionCodeForCountryCode(countryCallingCode);

  // Metadata cannot be null because the country calling code is valid
  PhoneMetadata metadata =
      _getMetadataForRegionOrCallingCode(countryCallingCode, regionCode)!;

  String formattedNumber = '';

  NumberFormat? formattingPattern = chooseFormattingPatternForNumber(
    userDefinedFormats,
    nationalSignificantNumber,
  );

  if (formattingPattern == null) {
    // If no pattern above is matched, we format the number as a whole.
    formattedNumber = nationalSignificantNumber;
  } else {
    // Before we do a replacement of the national prefix pattern $NP with the
    // national prefix, we need to copy the rule so that subsequent replacements
    // for different numbers have the appropriate national prefix.
    NumberFormat numFormatCopy = formattingPattern.deepCopy();
    String nationalPrefixFormattingRule =
        formattingPattern.nationalPrefixFormattingRule;
    if (nationalPrefixFormattingRule.isNotEmpty) {
      String nationalPrefix = metadata.nationalPrefix;
      if (nationalPrefix.isNotEmpty) {
        // Replace $NP with national prefix and $FG with the first group ($1).
        nationalPrefixFormattingRule = nationalPrefixFormattingRule
            .replaceFirst(_npString, nationalPrefix)
            .replaceFirst(_fgString, '\$1');
        numFormatCopy.nationalPrefixFormattingRule =
            nationalPrefixFormattingRule;
      } else {
        // We don't want to have a rule for how to format the national prefix if
        // there isn't one.
        numFormatCopy.clearNationalPrefixFormattingRule();
      }
    }

    formattedNumber = formatNsnUsingPattern(
      nationalSignificantNumber,
      numFormatCopy,
      numberFormat,
    );
  }

  String formattedExtension = _maybeGetFormattedExtension(
    number,
    metadata,
    numberFormat,
  );

  return _prefixNumberWithCountryCallingCode(
    countryCallingCode,
    numberFormat,
    formattedNumber,
    formattedExtension,
  );
}