Period.fromGooglePlay constructor

Period.fromGooglePlay(
  1. String periodText
)

This will parse the Google Play Response for the Period

Props to Pascal for this nicely written RegExpr.

Implementation

factory Period.fromGooglePlay(String periodText) {
  if (periodText.isEmpty) {
    throw ArgumentError("PeriodText is Empty");
  }
  //
  final regex = RegExp(r'(\d+)([A-Z])');
  final match = regex.firstMatch(periodText);
  final amount = int.parse(match!.group(1)!);
  final unitString = match.group(2);

  return Period._(
      amount: amount,
      periodUnit: switch (unitString) {
        "Y" => PeriodUnit.YEARS,
        "M" => PeriodUnit.MONTH,
        "W" => PeriodUnit.WEEKS,
        "D" => PeriodUnit.DAYS,
        _ => throw UnsupportedError("Period not supported")
      });
}