philiprehberger_collection_ext

Tests pub package Last updated

Iterable and Map extensions for groupBy, chunk, zip, partition, sliding, frequencies, and more

Requirements

  • Dart >= 3.6

Installation

Add to your pubspec.yaml:

dependencies:
  philiprehberger_collection_ext: ^0.5.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]

partition

final (evens, odds) = [1, 2, 3, 4, 5].partition((n) => n.isEven);
// evens: [2, 4], odds: [1, 3, 5]

intersperse

final result = [1, 2, 3].intersperse(0).toList();
// [1, 0, 2, 0, 3]

sliding

final windows = [1, 2, 3, 4].sliding(2).toList();
// [[1, 2], [2, 3], [3, 4]]

final stepped = [1, 2, 3, 4].sliding(2, step: 2).toList();
// [[1, 2], [3, 4]]

sumBy / averageBy

final total = [1, 2, 3].sumBy((n) => n);     // 6
final avg = [2, 4, 6].averageBy((n) => n);    // 4.0

frequencies

final counts = ['a', 'b', 'a', 'c', 'a'].frequencies();
// {a: 3, b: 1, c: 1}

associateBy

final byInitial = ['apple', 'banana', 'cherry'].associateBy((s) => s[0]);
// {a: apple, b: banana, c: cherry}

mapIndexed

final labeled = ['a', 'b', 'c'].mapIndexed((i, e) => '$i:$e').toList();
// ['0:a', '1:b', '2:c']

whereIndexed

final evens = [10, 20, 30, 40].whereIndexed((i, e) => i.isEven).toList();
// [10, 30]

flatMap

final flat = [[1, 2], [3, 4]].flatMap((e) => e).toList();
// [1, 2, 3, 4]

none

final allOdd = [1, 3, 5].none((n) => n.isEven); // true

takeWhileInclusive

final items = [1, 2, 3, 4, 5].takeWhileInclusive((n) => n < 3).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}

final doubled = {'a': 1, 'b': 2}.mapValues((v) => v * 10);
// {a: 10, b: 20}

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
partition(predicate) Split elements into matching and non-matching lists
intersperse(separator) Insert a separator between each pair of elements
sliding(size, step) Overlapping windows of elements
sumBy(selector) Sum of values by a selector function
averageBy(selector) Average of values by a selector function
frequencies() Count occurrences of each element
associateBy(keySelector) Create a map from elements using a key selector
mapIndexed(transform) Transform elements with access to their index
whereIndexed(test) Filter elements with access to their index
flatMap(transform) Map each element to an iterable and flatten
none(test) Returns true if no element satisfies the predicate
takeWhileInclusive(test) Like takeWhile but includes the first failing element

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
mapValues(transform) Transform values while keeping keys

Development

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

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Libraries

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