philiprehberger_collection_ext

Tests pub package Last updated

Iterable and Map extensions for groupBy, chunk, zip, distinct, and more

Requirements

  • Dart >= 3.6

Installation

Add to your pubspec.yaml:

dependencies:
  philiprehberger_collection_ext: ^0.1.0

Then run:

dart pub get

Usage

import 'package:philiprehberger_collection_ext/collection_ext.dart';

groupBy

final grouped = [1, 2, 3, 4, 5, 6].groupBy((n) => n.isEven ? 'even' : 'odd');
// {odd: [1, 3, 5], even: [2, 4, 6]}

chunk

final chunks = [1, 2, 3, 4, 5].chunk(2);
// [[1, 2], [3, 4], [5]]

zip

final pairs = [1, 2, 3].zip(['a', 'b', 'c']).toList();
// [(1, 'a'), (2, 'b'), (3, 'c')]

distinctBy

final unique = [1, 2, 3, 2, 1].distinctBy((n) => n).toList();
// [1, 2, 3]

Map Extensions

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}

API

Iterable Extensions

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

Map Extensions

Method Description
filterKeys(test) Filter entries by key predicate
filterValues(test) Filter entries by value predicate
mapKeys(transform) Transform keys while keeping values

Development

dart pub get
dart analyze --fatal-infos
dart test

Support

If you find this project useful:

License

MIT

Libraries

collection_ext
philiprehberger_collection_ext
Iterable and Map extensions for groupBy, chunk, zip, distinct, and more.