deepIntersectionByValue method

Map deepIntersectionByValue(
  1. Map toCompare
)

Returns new instance of Map containing all values existing 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 values: A ⋂ B

Implementation

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

  return intersection;
}