Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/collection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 1.20.0-wip

- Adds `Map.pairs` extension method to make it easier to work with maps using records rather than MapEntry.
- Adds `separated` and `separatedList` extension methods to `Iterable`.
- Adds `separate` extension method to `List`
- Add `IterableMapEntryExtension` for working on `Map` as a list of pairs, using
Expand Down
1 change: 1 addition & 0 deletions pkgs/collection/lib/collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export 'src/functions.dart';
export 'src/iterable_extensions.dart';
export 'src/iterable_zip.dart';
export 'src/list_extensions.dart';
export 'src/map_extensions.dart';
export 'src/priority_queue.dart';
export 'src/queue_list.dart';
export 'src/union_set.dart';
Expand Down
8 changes: 8 additions & 0 deletions pkgs/collection/lib/src/map_extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

extension MapExtensions<K, V> on Map<K, V> {
/// Like [Map.entries], but returns each entry as a record instead of a [MapEntry].

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs should say what it is from scratch, not start out relative to something else.
Iterables don't "return" anything.

Consider:

/// The key and value pairs of this map. 
///
/// Contains `(key, value)` for each key `key`
/// and it's associated value, in key iteration
/// order.

Should probably also document that contains uses equality of record pairs, it does not use the map's lookup, and it's not efficient.

(The .map iterable should have efficient length, but not much else.)

Iterable<(K, V)> get pairs => entries.map((entry) => (entry.key, entry.value));
}
13 changes: 13 additions & 0 deletions pkgs/collection/test/extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2393,6 +2393,19 @@ void main() {
});
});
});

group('Map', () {
group('pairs', () {
test('returns an empty list for an empty map', () {
expect(const <int, int>{}.pairs, isEmpty);
});

test('returns a pair for every entry in the map', () {
expect(const {1: 2, 3: 4, 5: 6}.pairs,
equals(const [(1, 2), (3, 4), (5, 6)]));
});
});
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little sparsely tested.
Try with maps that don't use normal equality.
Maybe an identity map with two equal-but-district keys.

}

/// Creates a plain iterable not implementing any other class.
Expand Down
Loading