Iterable and Map extensions for groupBy, chunk, zip, distinct, and more
dart pub add philiprehberger_collection_extIterable and Map extensions for groupBy, chunk, zip, distinct, and more
Add to your pubspec.yaml:
dependencies:
philiprehberger_collection_ext: ^0.1.1
Then run:
dart pub get
import 'package:philiprehberger_collection_ext/collection_ext.dart';
final grouped = [1, 2, 3, 4, 5, 6].groupBy((n) => n.isEven ? 'even' : 'odd');
// {odd: [1, 3, 5], even: [2, 4, 6]}
final chunks = [1, 2, 3, 4, 5].chunk(2);
// [[1, 2], [3, 4], [5]]
final pairs = [1, 2, 3].zip(['a', 'b', 'c']).toList();
// [(1, 'a'), (2, 'b'), (3, 'c')]
final unique = [1, 2, 3, 2, 1].distinctBy((n) => n).toList();
// [1, 2, 3]
final filtered = {'a': 1, 'b': 2, 'c': 3}.filterValues((v) => v > 1);
// {b: 2, c: 3}
final uppered = {'a': 1, 'b': 2}.mapKeys((k) => k.toUpperCase());
// {A: 1, B: 2}
| Method | Description |
|---|---|
groupBy(key) | Group elements by a key function |
countBy(key) | Count elements by a key function |
chunk(size) | Split into chunks of the given size |
distinctBy(key) | Remove duplicates by a key function |
minBy(key) | Find element with minimum key value |
maxBy(key) | Find element with maximum key value |
zip(other) | Pair elements with another iterable |
firstWhereOrNull(test) | Safe lookup returning null instead of throwing |
sortedBy(key) | Return a sorted copy by a key function |
| Method | Description |
|---|---|
filterKeys(test) | Filter entries by key predicate |
filterValues(test) | Filter entries by value predicate |
mapKeys(transform) | Transform keys while keeping values |
dart pub get
dart analyze --fatal-infos
dart test
If you find this project useful: