parseFromInlineMap<K, V> function

Map<K, V>? parseFromInlineMap<K, V>(
  1. String? s,
  2. Pattern delimiterPairs,
  3. Pattern delimiterKeyValue, [
  4. StringMapper<K>? mapperKey,
  5. StringMapper<V>? mapperValue,
  6. Map<K, V>? def,
])

Parses s as a inline Map.

delimiterPairs The delimiter for pairs. delimiterKeyValue The delimiter for keys and values. mapperKey Maps keys to another type. mapperValue Maps values to another type.

Implementation

Map<K, V>? parseFromInlineMap<K, V>(
    String? s, Pattern delimiterPairs, Pattern delimiterKeyValue,
    [StringMapper<K>? mapperKey,
    StringMapper<V>? mapperValue,
    Map<K, V>? def]) {
  if (s == null) return def;
  s = s.trim();
  if (s.isEmpty) return def;

  mapperKey ??= (k) => k as K;
  mapperValue ??= (v) => v as V;

  var pairs = s.split(delimiterPairs);

  var map = <K, V>{};

  for (var pair in pairs) {
    var entry = split(pair, delimiterKeyValue, 2);
    var k = mapperKey(entry[0]);
    var v = mapperValue(entry.length > 1 ? entry[1] : '');
    map[k] = v;
  }

  return map;
}