matchNationalNumber static method

bool matchNationalNumber(
  1. String number,
  2. PhoneNumberDesc numberDesc,
  3. bool allowPrefixMatch
)

Returns whether the given national number (a string containing only decimal digits) matches the national number pattern defined in the given PhoneNumberDesc message.

Implementation

static bool matchNationalNumber(
    String number, PhoneNumberDesc numberDesc, bool allowPrefixMatch) {
  String nationalNumberPattern = numberDesc.nationalNumberPattern;
  // We don't want to consider it a prefix match when
  // matching non-empty input against an empty pattern.
  if (nationalNumberPattern.isEmpty) return false;

  RegExp pattern = RegExp(nationalNumberPattern);
  if (pattern.matchAsPrefix(number) == null) return false;

  pattern = RegExp("^($nationalNumberPattern)\$");
  return pattern.firstMatch(number)?.group(0) == number || allowPrefixMatch;
}