maybeStripExtension method

String maybeStripExtension(
  1. StringBuffer number
)

Strips any extension (as in, the part of the number dialled after the call is connected, usually indicated with extn, ext, x or similar) from the end of the number, and returns it.

number the non-normalized telephone number that we wish to strip the extension from. returns the phone extension.

Implementation

String maybeStripExtension(StringBuffer number) {
  RegExpMatch? m = _extnPattern.firstMatch(number.toString());
  // If we find a potential extension, and the number preceding this is a viable
  // number, we assume it is an extension.
  if (m != null &&
      isViablePhoneNumber(number.toString().substring(0, m.start))) {
    // The numbers are captured into groups in the regular expression.
    for (int i = 1, length = m.groupCount; i <= length; i++) {
      if (m.group(i) != null) {
        // We go through the capturing groups until we find one that captured some
        // digits. If none
        // did, then we will return the empty string.
        String extension = m.group(i)!;
        String remainder =
            number.toString().replaceRange(m.start, number.length, '');
        number.clear();
        number.write(remainder);
        return extension;
      }
    }
  }
  return '';
}