apply method

String apply(
  1. String inputString
)

Apply the given phone mask to the input string.

Implementation

String apply(final String inputString) {
  /// If mask is empty, return input string
  if (mask.isEmpty) {
    return inputString;
  }

  final chars = inputString.replaceAll(RegExp(r'\D+'), '').split('');
  final result = <String>[];
  var index = 0;
  for (var i = 0; i < mask.length; i++) {
    if (index >= chars.length) {
      break;
    }
    final curChar = chars[index];
    if (mask[i] == '0') {
      /// If it's a digit in the mask, add the digit to the output
      if (_isDigit(curChar)) {
        result.add(curChar);
        index++;
      } else {
        break;
      }
    } else {
      /// Add the non-digit value from the mask to the output
      result.add(mask[i]);
    }
  }
  return result.join();
}