equatable v0.1.8
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 38356d5..f7d8d44 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -35,3 +35,7 @@
# 0.1.7
Added `toString` override
+
+# 0.1.8
+
+Support `Iterable` props
diff --git a/lib/src/equatable.dart b/lib/src/equatable.dart
index c67b72e..fee30ab 100644
--- a/lib/src/equatable.dart
+++ b/lib/src/equatable.dart
@@ -1,5 +1,3 @@
-import 'package:collection/collection.dart';
-
/// A class that helps implement equality
/// without needing to explicitly override == and [hashCode].
/// Equatables override their own == and [hashCode] based on
@@ -20,7 +18,7 @@
identical(this, other) ||
other is Equatable &&
runtimeType == other.runtimeType &&
- ListEquality().equals(props, other.props);
+ _equals(props, other.props);
@override
int get hashCode => runtimeType.hashCode ^ _propsHashCode;
@@ -35,6 +33,21 @@
return hashCode;
}
+ bool _equals(list1, list2) {
+ if (identical(list1, list2)) return true;
+ if (list1 == null || list2 == null) return false;
+ int length = list1.length;
+ if (length != list2.length) return false;
+ for (int i = 0; i < length; i++) {
+ if (list1[i] is Iterable) {
+ if (!_equals(list1[i], list2[i])) return false;
+ } else {
+ if (list1[i] != list2[i]) return false;
+ }
+ }
+ return true;
+ }
+
@override
String toString() => props.toString();
}
diff --git a/pubspec.yaml b/pubspec.yaml
index ca3a92e..5582714 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,6 +1,6 @@
name: equatable
description: An abstract class that helps to implement equality without needing to explicitly override == and hashCode.
-version: 0.1.7
+version: 0.1.8
author: felix.angelov <felangelov@gmail.com>
homepage: https://github.com/felangel/equatable
diff --git a/test/equatable_test.dart b/test/equatable_test.dart
index b19e183..0121a40 100644
--- a/test/equatable_test.dart
+++ b/test/equatable_test.dart
@@ -31,9 +31,10 @@
final String name;
final int age;
final Color hairColor;
+ final List<String> children;
- ComplexEquatable({this.name, this.age, this.hairColor})
- : super([name, age, hairColor]);
+ ComplexEquatable({this.name, this.age, this.hairColor, this.children})
+ : super([name, age, hairColor, children]);
}
class EquatableData extends Equatable {
@@ -303,14 +304,16 @@
name: 'Joe',
age: 40,
hairColor: Color.black,
+ children: ['Bob'],
);
- expect(instance.toString(), '[Joe, 40, Color.black]');
+ expect(instance.toString(), '[Joe, 40, Color.black, [Bob]]');
});
test('should return true when instance is the same', () {
final instance = ComplexEquatable(
name: 'Joe',
age: 40,
hairColor: Color.black,
+ children: ['Bob'],
);
expect(instance == instance, true);
});
@@ -320,13 +323,15 @@
name: 'Joe',
age: 40,
hairColor: Color.black,
+ children: ['Bob'],
);
expect(
instance.hashCode,
instance.runtimeType.hashCode ^
instance.name.hashCode ^
instance.age.hashCode ^
- instance.hairColor.hashCode,
+ instance.hairColor.hashCode ^
+ instance.children.hashCode,
);
});
@@ -335,11 +340,13 @@
name: 'Joe',
age: 40,
hairColor: Color.black,
+ children: ['Bob'],
);
final instanceB = ComplexEquatable(
name: 'Joe',
age: 40,
hairColor: Color.black,
+ children: ['Bob'],
);
expect(instanceA == instanceB, true);
});
@@ -349,6 +356,7 @@
name: 'Joe',
age: 40,
hairColor: Color.black,
+ children: ['Bob'],
);
final instanceB = NonEquatable();
expect(instanceA == instanceB, false);
@@ -359,11 +367,13 @@
name: 'Joe',
age: 40,
hairColor: Color.black,
+ children: ['Bob'],
);
final instanceB = ComplexEquatable(
name: 'John',
- age: 46,
+ age: 40,
hairColor: Color.brown,
+ children: ['Bobby'],
);
expect(instanceA == instanceB, false);
});