Skip to content

Commit e4e580f

Browse files
authored
fix: explicitly handle num equality comparisons (#189)
1 parent 49c8604 commit e4e580f

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/src/equatable_utils.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ bool iterableEquals(Iterable<Object?> a, Iterable<Object?> b) {
2121
return true;
2222
}
2323

24+
/// Determines whether two numbers are equal.
25+
@pragma('vm:prefer-inline')
26+
bool numEquals(num a, num b) => a == b;
27+
2428
/// Determines whether two sets are equal.
2529
bool setEquals(Set<Object?> a, Set<Object?> b) {
2630
if (identical(a, b)) return true;
@@ -45,7 +49,9 @@ bool mapEquals(Map<Object?, Object?> a, Map<Object?, Object?> b) {
4549
@pragma('vm:prefer-inline')
4650
bool objectsEquals(Object? a, Object? b) {
4751
if (identical(a, b)) return true;
48-
if (_isEquatable(a) && _isEquatable(b)) {
52+
if (a is num && b is num) {
53+
return numEquals(a, b);
54+
} else if (_isEquatable(a) && _isEquatable(b)) {
4955
return a == b;
5056
} else if (a is Set && b is Set) {
5157
return setEquals(a, b);

test/equatable_utils_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,5 +262,22 @@ void main() {
262262
test('returns false for different types', () {
263263
expect(objectsEquals(1, '1'), isFalse);
264264
});
265+
266+
test('returns true when two nums have the same value (int and double)', () {
267+
const num a = 0;
268+
const num b = 0.0;
269+
expect(objectsEquals(a, b), isTrue);
270+
});
271+
272+
test('returns true when two nums are identical', () {
273+
const num a = 0;
274+
expect(objectsEquals(a, a), isTrue);
275+
});
276+
277+
test('returns false when two nums have different values', () {
278+
const num a = 0;
279+
const num b = 1;
280+
expect(objectsEquals(a, b), isFalse);
281+
});
265282
});
266283
}

0 commit comments

Comments
 (0)