truncateTooLongNumber method

bool truncateTooLongNumber(
  1. PhoneNumber number
)

Attempts to extract a valid number from a phone number that is too long to be valid, and resets the PhoneNumber object passed in to that valid version. If no valid number could be extracted, the PhoneNumber object passed in will not be modified. number a PhoneNumber object which contains a number that is too long to be valid. truncateTooLongNumber returns true if a valid phone number can be successfully extracted.

Implementation

bool truncateTooLongNumber(PhoneNumber number) {
  if (isValidNumber(number)) return true;

  PhoneNumber numberCopy = number.deepCopy();
  int nationalNumber = number.nationalNumber.toInt();

  do {
    nationalNumber = (nationalNumber / 10).floor();
    numberCopy.nationalNumber = Int64(nationalNumber);
    if (nationalNumber == 0 ||
        isPossibleNumberWithReason(numberCopy) == ValidationResult.tooShort) {
      return false;
    }
  } while (!isValidNumber(numberCopy));

  number.nationalNumber = Int64(nationalNumber);

  return true;
}