getLengthOfNationalDestinationCode method

int getLengthOfNationalDestinationCode(
  1. PhoneNumber number
)

Gets the length of the national destination code (NDC) from the PhoneNumber object passed in, so that clients could use it to split a national significant number into NDC and subscriber number. The NDC of a phone number is normally the first group of digit(s) right after the country calling code when the number is formatted in the international format, if there is a subscriber number part that follows.

N.B.: similar to an area code, not all numbers have an NDC!

An example of how this could be used:

var phoneUtil = PhoneNumberUtil.instance;
var number = phoneUtil.parse('18002530000', 'US');
var nationalSignificantNumber =
    phoneUtil.getNationalSignificantNumber(number);
var nationalDestinationCode;
var subscriberNumber;

var nationalDestinationCodeLength =
    phoneUtil.getLengthOfNationalDestinationCode(number);
if (nationalDestinationCodeLength > 0) {
  nationalDestinationCode =
      nationalSignificantNumber.substring(0, nationalDestinationCodeLength);
  subscriberNumber =
      nationalSignificantNumber.substring(nationalDestinationCodeLength);
} else {
  nationalDestinationCode = '';
  subscriberNumber = nationalSignificantNumber;
}

Refer to the unittests to see the difference between this function and getLengthOfGeographicalAreaCode.

PhoneNumber number is the PhoneNumber object for which clients want to know the length of the NDC. getLengthOfNationalDestinationCode returns the length of NDC of the PhoneNumber object passed in, which could be zero.

Implementation

int getLengthOfNationalDestinationCode(PhoneNumber number) {
  PhoneNumber copiedProto;
  if (number.hasExtension_3()) {
    // We don't want to alter the proto given to us, but we don't want to
    // include the extension when we format it, so we copy it and clear the
    // extension here.
    copiedProto = number.deepCopy();
    copiedProto.clearExtension_3();
  } else {
    copiedProto = number;
  }

  String nationalSignificantNumber =
      format(copiedProto, PhoneNumberFormat.international);

  List<String> numberGroups =
      nationalSignificantNumber.split(nonDigitsPattern);
  // The pattern will start with '+COUNTRY_CODE ' so the first group will always
  // be the empty string (before the + symbol) and the second group will be the
  // country calling code. The third group will be area code if it is not the
  // last group.
  // NOTE: On IE the first group that is supposed to be the empty string does
  // not appear in the array of number groups... so make the result on non-IE
  // browsers to be that of IE.
  if (numberGroups.first.isEmpty) numberGroups.removeAt(0);
  if (numberGroups.length <= 2) return 0;

  if (getNumberType(number) == PhoneNumberType.mobile) {
    // For example Argentinian mobile numbers, when formatted in the
    // international format, are in the form of +54 9 NDC XXXX.... As a result,
    // we take the length of the third group (NDC) and add the length of the
    // mobile token, which also forms part of the national significant number.
    // This assumes that the mobile token is always formatted separately from
    // the rest of the phone number.
    String mobileToken = getCountryMobileToken(number.countryCode);
    if (mobileToken != '') {
      return numberGroups[2].length + mobileToken.length;
    }
  }
  return numberGroups[1].length;
}