formatNsn static method

String formatNsn(
  1. String nsn,
  2. IsoCode isoCode, [
  3. NsnFormat format = NsnFormat.national
])

format national number for international use

Implementation

static String formatNsn(
  String nsn,
  IsoCode isoCode, [
  NsnFormat format = NsnFormat.national,
]) {
  if (nsn.isEmpty) {
    return nsn;
  }
  // since the phone number might be incomplete, fake digits
  // are temporarily added to format a complete number.
  final missingDigits = _getMissingDigits(nsn, isoCode);
  final completePhoneNumber = nsn + missingDigits;
  final formatingRules =
      MetadataFinder.findMetadataFormatsForIsoCode(isoCode);
  final formatingRule = _getMatchingFormatRules(
    formatingRules: formatingRules,
    nsn: completePhoneNumber,
  );
  // for incomplete phone number

  if (formatingRule == null) {
    return nsn;
  }
  var transformRule = formatingRule.format;
  // if there is an international format, we use it
  final intlFormat = formatingRule.intlFormat;
  if (format == NsnFormat.international &&
      intlFormat != null &&
      intlFormat != 'NA') {
    transformRule = intlFormat;
  }

  var formatted = NationalNumberParser.applyTransformRules(
    appliedTo: completePhoneNumber,
    pattern: formatingRule.pattern,
    transformRule: transformRule,
  );
  formatted = _removeMissingDigits(formatted, missingDigits);
  return formatted;
}