File tree Expand file tree Collapse file tree 5 files changed +63
-1
lines changed Expand file tree Collapse file tree 5 files changed +63
-1
lines changed Original file line number Diff line number Diff line change
1
+ ## 1.3.0
2
+ - Add ` distinctBy ` to iterable extensions.
3
+
1
4
## 1.2.0
2
5
- Add ` ifEmpty ` and ` ifBlank ` to string extensions.
3
6
- Remove typedefs. (private)
Original file line number Diff line number Diff line change 1
1
export 'date_time_extensions.dart' ;
2
2
export 'future_extensions.dart' ;
3
+ export 'iterable_extensions.dart' ;
3
4
export 'object_extensions.dart' ;
4
5
export 'string_extensions.dart' ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
name : netglade_utils
2
- version : 1.2 .0
2
+ version : 1.3 .0
3
3
description : Dart utils used internally at netglade.
4
4
repository : https://github.com/netglade/flutter_core/tree/main/packages/netglade_utils
5
5
issue_tracker : https://github.com/netglade/flutter_core/issues
@@ -12,6 +12,7 @@ environment:
12
12
dependencies :
13
13
characters : ^1.2.0
14
14
clock : ^1.0.0
15
+ collection : ^1.0.0
15
16
http : ^1.0.0
16
17
mocktail : ^1.0.0
17
18
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments