format static method

String format(
  1. String nsn,
  2. IsoCode isoCode, [
  3. PhoneNumberFormat format = PhoneNumberFormat.international
])

Formats nsn as format.

isoCode is the region the number belongs to, it provides both the dial code and the grouping rules.

Implementation

static String format(
  String nsn,
  IsoCode isoCode, [
  PhoneNumberFormat format = PhoneNumberFormat.international,
]) {
  final countryCode =
      MetadataFinder.findMetadataForIsoCode(isoCode).countryCode;
  if (nsn.isEmpty) {
    return switch (format) {
      PhoneNumberFormat.e164 => '+$countryCode',
      PhoneNumberFormat.international ||
      PhoneNumberFormat.national ||
      PhoneNumberFormat.nsn =>
        '',
    };
  }
  return switch (format) {
    PhoneNumberFormat.e164 => '+$countryCode$nsn',
    PhoneNumberFormat.international => '+$countryCode '
        '${_formatNsn(nsn, isoCode, NsnFormat.international)}',
    PhoneNumberFormat.national =>
      _formatNsn(nsn, isoCode, NsnFormat.national),
    PhoneNumberFormat.nsn =>
      _formatNsn(nsn, isoCode, NsnFormat.nationalWithoutPrefix),
  };
}