Skip to content

Commit

Permalink
fix: The value of key in Distinct is null-acceptable.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Apr 3, 2023
1 parent 9f25a38 commit 04a59d2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/katana/lib/extension/iterable_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ extension IterableExtensions<T> on Iterable<T> {
/// final array = [1, 1, 3, 5, 6, 8, 9, 9];
/// final distinct = array.distinct(); // [1, 3, 5, 6, 8, 9]
/// ```
List<T> distinct([Object Function(T element)? key]) {
List<T> distinct([Object? Function(T element)? key]) {
if (key == null) {
return toSet().toList();
}
final tmp = <Object, T>{};
final tmp = <Object?, T>{};
for (final element in this) {
final o = key.call(element);
if (tmp.containsKey(o)) {
Expand Down
3 changes: 3 additions & 0 deletions packages/katana/test/iterable_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ void main() {
final array = [1, 1, 3, 5, 6, 8, 9, 9];
expect(array.distinct(), [1, 3, 5, 6, 8, 9]);
expect(array.distinct((i) => i % 3), [1, 3, 5]);
final array2 = [1, null, 1, 3, 6, 9, null, 9];
expect(array2.distinct(), [1, null, 3, 6, 9]);
expect(array2.distinct((i) => i % 3), [1, null, 3]);
});
test("IterableExtensions.firstOrNull", () {
final array = [1, 1, 3, 5, 6, 8, 9, 9];
Expand Down

0 comments on commit 04a59d2

Please sign in to comment.