Skip to content

Commit e09a893

Browse files
authored
Merge pull request #24 from netglade/story/iterable-distinct
Add distinctBy iterable extension
2 parents 5b055d4 + aa3261d commit e09a893

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

packages/netglade_utils/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 1.3.0
2+
- Add `distinctBy` to iterable extensions.
3+
14
## 1.2.0
25
- Add `ifEmpty` and `ifBlank` to string extensions.
36
- Remove typedefs. (private)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export 'date_time_extensions.dart';
22
export 'future_extensions.dart';
3+
export 'iterable_extensions.dart';
34
export 'object_extensions.dart';
45
export 'string_extensions.dart';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'dart:core';
2+
3+
typedef Selector<T, K> = K Function(T selectFrom);
4+
5+
extension IterableExtensions<T> on Iterable<T> {
6+
/// Returns an iterable containing only elements from the given collection having distinct keys returned by the given [selector] function.
7+
/// If multiple elements have the same key, first element is returned.
8+
Iterable<T> distinctBy<K>(Selector<T, K> selector) {
9+
final result = <K, T>{};
10+
for (final item in this) {
11+
final _ = result.putIfAbsent(selector(item), () => item);
12+
}
13+
14+
return result.values;
15+
}
16+
}

packages/netglade_utils/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: netglade_utils
2-
version: 1.2.0
2+
version: 1.3.0
33
description: Dart utils used internally at netglade.
44
repository: https://github.com/netglade/flutter_core/tree/main/packages/netglade_utils
55
issue_tracker: https://github.com/netglade/flutter_core/issues
@@ -12,6 +12,7 @@ environment:
1212
dependencies:
1313
characters: ^1.2.0
1414
clock: ^1.0.0
15+
collection: ^1.0.0
1516
http: ^1.0.0
1617
mocktail: ^1.0.0
1718

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'package:collection/collection.dart';
2+
import 'package:netglade_utils/netglade_utils.dart';
3+
import 'package:test/test.dart';
4+
5+
void main() {
6+
group('distinct by', () {
7+
test('distinct', () {
8+
expect(
9+
[
10+
[1, 3],
11+
[2, 5],
12+
[3, 3],
13+
].distinctBy((e) => e.firstOrNull),
14+
equals({
15+
[1, 3],
16+
[2, 5],
17+
[3, 3],
18+
}),
19+
);
20+
});
21+
22+
test('with duplicates', () {
23+
expect(
24+
[
25+
[1, 3],
26+
[2, 5],
27+
[1, 6],
28+
[1, 0],
29+
].distinctBy((e) => e.firstOrNull),
30+
equals({
31+
[1, 3],
32+
[2, 5],
33+
}),
34+
);
35+
});
36+
37+
test('empty', () {
38+
expect(<int>[].distinctBy((e) => e * e), equals(<int>{}));
39+
});
40+
});
41+
}

0 commit comments

Comments
 (0)