cardsFromText function

Iterable<Card> cardsFromText(
  1. String text
)

Parse a string of card text into a sequence of cards. Whitespace is ignored between cards, but is not allowed between rank and suit. Cards may be enclosed in square brackets.

Implementation

Iterable<Card> cardsFromText(String text) sync* {
  var input = text;
  var match = _brackets.firstMatch(input);
  if (match != null && match.group(1)!.isNotEmpty) {
    input = match.group(1)!;
  }
  var matches = _oneCard.allMatches(input);
  for (var match in matches) {
    if (match.group(1) == null) {
      return;
    }
    if (match.group(1) == "Jk") {
      yield Card.Joker;
      continue;
    }
    if (match.group(1) == "Jb") {
      yield Card.BlackJoker;
      continue;
    }
    if (match.group(1) == "Jw") {
      yield Card.WhiteJoker;
      continue;
    }
    if (match.group(2) == null || match.group(3) == null) {
      return;
    }
    var r = Rank.fromChar(match.group(2)!);
    var s = Suit.fromChar(match.group(3)!);
    if (r == null || s == null) {
      return;
    }
    yield Card.fromRankSuit(r, s);
  }
}