stringToDigits method

List<int> stringToDigits(
  1. dynamic string
)

Implementation

List<int> stringToDigits(dynamic string) {
  if (string is String) {
    if (_isPrefixCode == null || !_isPrefixCode!) {
      throw Exception(
          'parsing string without prefix code is unsupported. Pass in array '
          'of stringy symbols?');
    }
    final re = RegExp('(${List.from(sym2num.keys).join('|')})');
    return re
        .allMatches(string)
        .map<String>((e) => e.input.substring(e.start, e.end))
        .map<int>((symbol) {
      final num = sym2num[symbol];
      if (num != null) return num;
      throw Exception('Undefined value for sym2num with key $symbol');
    }).toList();
  }

  if (string is List<String>) {
    return string.map((e) {
      final num = sym2num[e];
      if (num != null) return num;
      throw Exception('Undefined value for sym2num with key $e');
    }).toList();
  }

  throw Exception("param must be of type String or List<String>");
}