parseMapEntryOfStrings<K, V> function

MapEntry<String, String>? parseMapEntryOfStrings<K, V>(
  1. Object? e, [
  2. Pattern? delimiter,
  3. MapEntry<String, String>? def
])

Parses e as a MapEntry<String, String>

Implementation

MapEntry<String, String>? parseMapEntryOfStrings<K, V>(Object? e,
    [Pattern? delimiter, MapEntry<String, String>? def]) {
  if (e == null) return def;

  if (e is Map) {
    if (e.isNotEmpty) {
      var elem = e.entries.first;
      if (elem.key == null) return def;
      return MapEntry<String, String>(
          parseString(elem.key, '')!, parseString(elem.value, '')!);
    } else {
      return def;
    }
  } else if (e is List) {
    if (e.isEmpty) return def;
    if (e.length == 1) {
      delimiter ??= RegExp(r'\s*;\s*');
      return parseMapEntryOfStrings(e.first, delimiter, def);
    } else if (e.length == 2) {
      return MapEntry('${e[0]}', '${e[1]}');
    } else {
      var values = e.sublist(1);
      return MapEntry('${e[0]}', values.join(','));
    }
  } else {
    delimiter ??= RegExp(r'\s*[,;:]\s*');
    var s = parseString(e)!;
    var list = split(s, delimiter, 2);
    if (list.length == 2) {
      return MapEntry(list[0], list[1]);
    } else {
      var first = list.first;
      if (first.length < s.length) {
        return MapEntry(first, '');
      } else {
        return def;
      }
    }
  }
}