Skip to content

Commit b9dc19c

Browse files
committed
Fix review comments
1 parent 672e51a commit b9dc19c

File tree

2 files changed

+140
-37
lines changed

2 files changed

+140
-37
lines changed

pkgs/collection/lib/src/iterable_extensions.dart

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -916,29 +916,39 @@ extension IterableIterableExtension<T> on Iterable<Iterable<T>> {
916916

917917
/// Extension on iterables of [MapEntry].
918918
///
919-
/// An [Iterable<MapEntry>] is obtained using [Map.entries], these extensions
920-
/// make it easy to work on a [Map] as a list of pairs.
919+
/// An [Iterable<MapEntry>] is obtained using [Map.entries]. These extensions
920+
/// facilitates working directly on the entries of a [Map].
921921
extension IterableMapEntryExtension<K, V> on Iterable<MapEntry<K, V>> {
922-
/// Creates a new lazy [Iterable] with all elements whose [MapEntry.key]
923-
/// satisfy the predicate [test].
922+
/// The elements whose [MapEntry.key] values satisfy [test].
923+
///
924+
/// The resulting iterable is lazily computing its elements
925+
/// based on the elements this iterable.
924926
Iterable<MapEntry<K, V>> whereKey(bool Function(K) test) =>
925927
where((e) => test(e.key));
926928

927-
/// Creates a new lazy [Iterable] with all elements whose [MapEntry.value]
928-
/// satisfy the predicate [test].
929+
/// The elements whose [MapEntry.value] values satisfy [test].
930+
///
931+
/// The resulting iterable is lazily computing its elements
932+
/// based on the elements this iterable.
929933
Iterable<MapEntry<K, V>> whereValue(bool Function(V) test) =>
930934
where((e) => test(e.value));
931935

932-
/// Create an new lazy [Iterable] with [MapEntry.key] from all elements.
936+
/// A new lazy [Iterable] of the [MapEntry.key]s of these entries.
937+
///
938+
/// Do not use this getter as `map.entries.keys`, just use `map.keys`
939+
/// directly.
933940
Iterable<K> get keys => map((e) => e.key);
934941

935-
/// Create an new lazy [Iterable] with [MapEntry.value] from all elements.
942+
/// A new lazy [Iterable] of the [MapEntry.value]s of these entries.
943+
///
944+
/// Do not use this getter as `map.entries.values`, just use `map.values`
945+
/// directly.
936946
Iterable<V> get values => map((e) => e.value);
937947

938-
/// Create a [Map] from all elements.
948+
/// Create a [Map<K, V>] from all elements.
939949
///
940950
/// This is a short-hand for [Map.fromEntries].
941-
Map<K, V> toMap() => Map.fromEntries(this);
951+
Map<K, V> toMap() => Map<K, V>.fromEntries(this);
942952
}
943953

944954
/// Extensions that apply to iterables of [Comparable] elements.

pkgs/collection/test/extensions_test.dart

Lines changed: 120 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,81 +1125,174 @@ void main() {
11251125
group('of MapEntry', () {
11261126
group('.whereKey', () {
11271127
test('empty', () {
1128-
expect(<String, int>{}.entries.whereKey(unreachable), isEmpty);
1128+
expect(
1129+
iterable(<MapEntry<String, int>>[]).whereKey(unreachable),
1130+
isEmpty,
1131+
);
11291132
});
11301133
test('single', () {
1131-
expect({'a': 1}.entries.whereKey((k) => k == 'a').toMap(), {'a': 1});
1132-
expect({'a': 1}.entries.whereKey((k) => k == 'b').toMap(), isEmpty);
1134+
expect(
1135+
iterable([const MapEntry('a', 1)]).whereKey((k) => k == 'a'),
1136+
[const MapEntry('a', 1)],
1137+
);
1138+
expect(
1139+
iterable([const MapEntry('a', 1)]).whereKey((k) => k == 'b'),
1140+
isEmpty,
1141+
);
11331142
});
11341143
test('multiple', () {
11351144
expect(
1136-
{'a': 1, 'b': 2}.entries.whereKey((k) => k == 'a').toMap(),
1137-
{'a': 1},
1145+
iterable([
1146+
const MapEntry('a', 1),
1147+
const MapEntry('b', 2),
1148+
]).whereKey((k) => k == 'a'),
1149+
[const MapEntry('a', 1)],
11381150
);
11391151
expect(
1140-
{'a': 1, 'b': 2}.entries.whereKey((k) => k == 'b').toMap(),
1141-
{'b': 2},
1152+
iterable([
1153+
const MapEntry('a', 1),
1154+
const MapEntry('b', 2),
1155+
]).whereKey((k) => k == 'b'),
1156+
[const MapEntry('b', 2)],
11421157
);
11431158
expect(
1144-
{'a': 1, 'b': 2}.entries.whereKey((k) => k != 'c').toMap(),
1145-
{'a': 1, 'b': 2},
1159+
iterable([
1160+
const MapEntry('a', 1),
1161+
const MapEntry('b', 2),
1162+
]).whereKey((k) => k != 'c'),
1163+
[const MapEntry('a', 1), const MapEntry('b', 2)],
1164+
);
1165+
expect(
1166+
iterable([
1167+
const MapEntry('a', 1),
1168+
const MapEntry('b', 2),
1169+
const MapEntry('a', 3),
1170+
]).whereKey((k) => k == 'a'),
1171+
[const MapEntry('a', 1), const MapEntry('a', 3)],
11461172
);
11471173
});
11481174
});
11491175
group('.whereValue', () {
11501176
test('empty', () {
1151-
expect(<String, int>{}.entries.whereValue(unreachable), isEmpty);
1177+
expect(
1178+
iterable(<MapEntry<String, int>>[]).whereValue(unreachable),
1179+
isEmpty,
1180+
);
11521181
});
11531182
test('single', () {
1154-
expect({'a': 1}.entries.whereValue((v) => v == 1).toMap(), {'a': 1});
1155-
expect({'a': 1}.entries.whereValue((v) => v == 2).toMap(), isEmpty);
1183+
expect(
1184+
iterable([const MapEntry('a', 1)]).whereValue((v) => v == 1),
1185+
[const MapEntry('a', 1)],
1186+
);
1187+
expect(
1188+
iterable([const MapEntry('a', 1)]).whereValue((v) => v == 2),
1189+
isEmpty,
1190+
);
11561191
});
11571192
test('multiple', () {
11581193
expect(
1159-
{'a': 1, 'b': 2}.entries.whereValue((v) => v == 1).toMap(),
1160-
{'a': 1},
1194+
iterable([
1195+
const MapEntry('a', 1),
1196+
const MapEntry('b', 2),
1197+
]).whereValue((v) => v == 1),
1198+
[const MapEntry('a', 1)],
11611199
);
11621200
expect(
1163-
{'a': 1, 'b': 2}.entries.whereValue((v) => v == 2).toMap(),
1164-
{'b': 2},
1201+
iterable([
1202+
const MapEntry('a', 1),
1203+
const MapEntry('b', 2),
1204+
]).whereValue((v) => v == 2),
1205+
[const MapEntry('b', 2)],
11651206
);
11661207
expect(
1167-
{'a': 1, 'b': 2}.entries.whereValue((v) => v != 3).toMap(),
1168-
{'a': 1, 'b': 2},
1208+
iterable([
1209+
const MapEntry('a', 1),
1210+
const MapEntry('b', 2),
1211+
]).whereValue((v) => v != 3),
1212+
[const MapEntry('a', 1), const MapEntry('b', 2)],
1213+
);
1214+
expect(
1215+
iterable([
1216+
const MapEntry('a', 1),
1217+
const MapEntry('b', 2),
1218+
const MapEntry('c', 1),
1219+
]).whereValue((v) => v == 1),
1220+
[const MapEntry('a', 1), const MapEntry('c', 1)],
1221+
);
1222+
expect(
1223+
iterable([
1224+
const MapEntry('a', 1),
1225+
const MapEntry('b', 2),
1226+
const MapEntry('a', 1),
1227+
]).whereValue((v) => v == 1),
1228+
[const MapEntry('a', 1), const MapEntry('a', 1)],
11691229
);
11701230
});
11711231
});
11721232
group('.keys', () {
11731233
test('empty', () {
1174-
expect(<String, int>{}.entries.keys, isEmpty);
1234+
expect(iterable(<MapEntry<String, int>>[]).keys, isEmpty);
11751235
});
11761236
test('single', () {
1177-
expect({'a': 1}.entries.keys, ['a']);
1237+
expect(iterable([const MapEntry('a', 1)]).keys, ['a']);
11781238
});
11791239
test('multiple', () {
1180-
expect({'a': 1, 'b': 2}.entries.keys, ['a', 'b']);
1240+
expect(
1241+
iterable([const MapEntry('a', 1), const MapEntry('b', 2)]).keys,
1242+
['a', 'b'],
1243+
);
1244+
expect(
1245+
iterable([
1246+
const MapEntry('a', 1),
1247+
const MapEntry('b', 2),
1248+
const MapEntry('a', 3),
1249+
]).keys,
1250+
['a', 'b', 'a'],
1251+
);
11811252
});
11821253
});
11831254
group('.values', () {
11841255
test('empty', () {
1185-
expect(<String, int>{}.entries.values, isEmpty);
1256+
expect(iterable(<MapEntry<String, int>>[]).values, isEmpty);
11861257
});
11871258
test('single', () {
1188-
expect({'a': 1}.entries.values, [1]);
1259+
expect(iterable([const MapEntry('a', 1)]).values, [1]);
11891260
});
11901261
test('multiple', () {
1191-
expect({'a': 1, 'b': 2}.entries.values, [1, 2]);
1262+
expect(
1263+
iterable([const MapEntry('a', 1), const MapEntry('b', 2)]).values,
1264+
[1, 2],
1265+
);
1266+
expect(
1267+
iterable([
1268+
const MapEntry('a', 1),
1269+
const MapEntry('b', 2),
1270+
const MapEntry('a', 3),
1271+
]).values,
1272+
[1, 2, 3],
1273+
);
11921274
});
11931275
});
11941276
group('.toMap', () {
11951277
test('empty', () {
1196-
expect(<String, int>{}.entries.toMap(), <String, int>{});
1278+
expect(iterable(<MapEntry<String, int>>[]).toMap(), <String, int>{});
11971279
});
11981280
test('single', () {
1199-
expect({'a': 1}.entries.toMap(), {'a': 1});
1281+
expect(iterable([const MapEntry('a', 1)]).toMap(), {'a': 1});
12001282
});
12011283
test('multiple', () {
1202-
expect({'a': 1, 'b': 2}.entries.toMap(), {'a': 1, 'b': 2});
1284+
expect(
1285+
iterable([const MapEntry('a', 1), const MapEntry('b', 2)]).toMap(),
1286+
{'a': 1, 'b': 2},
1287+
);
1288+
expect(
1289+
iterable([
1290+
const MapEntry('a', 1),
1291+
const MapEntry('b', 2),
1292+
const MapEntry('a', 3),
1293+
]).toMap(),
1294+
{'b': 2, 'a': 3},
1295+
);
12031296
});
12041297
});
12051298
});

0 commit comments

Comments
 (0)