rodydavis / signals.dart

Reactive programming made simple for Dart and Flutter
http://dartsignals.dev
Apache License 2.0
378 stars 44 forks source link

Added deep collection equality for signal comparison #256

Closed leoafarias closed 2 months ago

leoafarias commented 2 months ago

Issue: #243

This will be a two part PR:

This adds a new utility class DeepCollectionEquality to the signals_core package. The class provides a way to compare the equality of complex data structures, such as maps, sets, and iterables, by recursively comparing their elements.

The DeepCollectionEquality class includes:

  1. equals method: Compares two objects for deep equality, handling maps, sets, and iterables.
  2. _mapsEqual method: Compares two maps for deep equality.
  3. _iterablesEqual method: Compares two iterables for deep equality, considering element order.
  4. _setsEqual method: Compares two sets for deep equality.

Example usage:

final deepEquality = DeepCollectionEquality();
final map1 = {'a': 1, 'b': [2, 3]};
final map2 = {'b': [2, 3], 'a': 1};
print(deepEquality.equals(map1, map2)); // Output: true

If things look good so far, let me know where you would like to add the functionality and I can give it a go.

rodydavis commented 2 months ago

Do we need unordered list/set comparisons? Or just have a method to pass in that can override this implementation?

leoafarias commented 2 months ago

@rodydavis overriding will be your call. Initially I am trying to kep it consistent with flutter foundation library.

set: https://api.flutter.dev/flutter/foundation/setEquals.html map: https://api.flutter.dev/flutter/foundation/mapEquals.html list: https://api.flutter.dev/flutter/foundation/listEquals.html

rodydavis commented 2 months ago

Works for me! I was meaning like a static override on the class but could be added in a later PR

rodydavis commented 2 months ago

Also we can make the members public so the class can be extended and overridden.

Maybe the name could be prefixed with Signals too

leoafarias commented 2 months ago

Hey @rodydavis let me know how you want to proceed with the remaining changes