deepIntersectionByKey method

Map deepIntersectionByKey(
  1. Map toCompare
)

Returns new instance of Map containing all values, which key exists in both maps. Does not work with nested List and Set yet.

According to complement in set theory (for clarity see Venn Diagrams): A - this B - toCompare Intersection in keys: A ⋂ B

Implementation

Map deepIntersectionByKey(Map toCompare) {
  var intersection = {};
  forEach((key, value) {
    if (toCompare.containsKey(key)) {
      if (value is Map && toCompare[key] is Map) {
        var deepIntersection = value.deepIntersectionByKey(toCompare[key]);
        if (deepIntersection.isNotEmpty) {
          intersection.addEntries([MapEntry(key, deepIntersection)]);
        }
      } else if (value is Map && toCompare[key] is! Map) {
        intersection.addEntries([MapEntry(key, {})]);
      } else {
        intersection.addEntries([MapEntry(key, value)]);
      }
    }
  });

  return intersection;
}