clean top-level constant

List<String> const clean

Implementation

const clean = [
  "/// this method will clean all null values from your map",
  "/// and will trim all string values",
  "Map<String, dynamic> clean(",
  "  Map? map, {",
  "  List<String> skipKeys = const [],",
  "  List<String> deleteKeys = const [],",
  "}) {",
  "  if (map == null) return {};",
  "  final copy = Map.from(map);",
  "  copy.removeWhere((key, value) {",
  "    if (skipKeys.contains(key)) return false;",
  "    if (deleteKeys.contains(key)) return true;",
  "    if (value == null) return true;",
  "    if (copy[key] is Map) {",
  "      copy[key] = clean(copy[key], skipKeys: skipKeys, deleteKeys: deleteKeys);",
  "      return copy[key].isEmpty;",
  "    }",
  "    if (value is List<Map>) {",
  "      copy[key] = value",
  "          .map((e) => clean(e, skipKeys: skipKeys, deleteKeys: deleteKeys))",
  "          .toList()",
  "        ..removeWhere((e) => e.isEmpty);",
  "      if (copy[key].isEmpty) return true;",
  "    }",
  "    if (value is List && value.isEmpty) {",
  "      return true;",
  "    }",
  "    return false;",
  "  });",
  "  return _trim(copy);",
  "}",
  "",
  "Map<String, dynamic> _trim(Map map) {",
  "  return map.map((key, value) {",
  "    var tmpValue = value;",
  "    if (value is String) {",
  "      tmpValue = value.trim();",
  "    } else if (value is Map) {",
  "      tmpValue = _trim(value);",
  "    } else if (value is List<Map>) {",
  "      tmpValue = value.cast<Map>().map((vMap) => _trim(vMap)).toList();",
  "    }",
  "    return MapEntry(key, tmpValue);",
  "  });",
  "}",
];