Skip to content

Commit

Permalink
fix: explicitly handle num equality comparisons (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel authored Nov 21, 2024
1 parent 49c8604 commit e4e580f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/src/equatable_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ bool iterableEquals(Iterable<Object?> a, Iterable<Object?> b) {
return true;
}

/// Determines whether two numbers are equal.
@pragma('vm:prefer-inline')
bool numEquals(num a, num b) => a == b;

/// Determines whether two sets are equal.
bool setEquals(Set<Object?> a, Set<Object?> b) {
if (identical(a, b)) return true;
Expand All @@ -45,7 +49,9 @@ bool mapEquals(Map<Object?, Object?> a, Map<Object?, Object?> b) {
@pragma('vm:prefer-inline')
bool objectsEquals(Object? a, Object? b) {
if (identical(a, b)) return true;
if (_isEquatable(a) && _isEquatable(b)) {
if (a is num && b is num) {
return numEquals(a, b);
} else if (_isEquatable(a) && _isEquatable(b)) {
return a == b;
} else if (a is Set && b is Set) {
return setEquals(a, b);
Expand Down
17 changes: 17 additions & 0 deletions test/equatable_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,5 +262,22 @@ void main() {
test('returns false for different types', () {
expect(objectsEquals(1, '1'), isFalse);
});

test('returns true when two nums have the same value (int and double)', () {
const num a = 0;
const num b = 0.0;
expect(objectsEquals(a, b), isTrue);
});

test('returns true when two nums are identical', () {
const num a = 0;
expect(objectsEquals(a, a), isTrue);
});

test('returns false when two nums have different values', () {
const num a = 0;
const num b = 1;
expect(objectsEquals(a, b), isFalse);
});
});
}

0 comments on commit e4e580f

Please sign in to comment.