parseDate function

List<String> parseDate(
  1. String expDateStr
)

Parses the string form of the expiration date and returns the month and year as a List<String>

Allows for the following date formats: 'MM/YY' 'MM/YYY' 'MM/YYYY'

This function will replace hyphens with slashes for dates that have hyphens in them and remove any whitespace

Implementation

List<String> parseDate(String expDateStr) {
  // Replace hyphens with slashes and remove whitespaces
  String formattedStr = expDateStr.replaceAll('-', '/')
    ..replaceAll(whiteSpaceRegex, '');

  Match? match = expDateFormat.firstMatch(formattedStr);

  if (match == null) {
    return [];
  }

  return match[0]!.split('/');
}