Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KMap.toMap, KMap.toMutableMap #7

Merged
merged 1 commit into from
Dec 2, 2018
Merged
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
11 changes: 11 additions & 0 deletions lib/src/extension/map_extensions_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ abstract class KMapExtensionsMixin<K, V> implements KMapExtension<K, V>, KMap<K,
return entries.associateByTo(destination, (it) => it.key, transform);
}

@override
KMap<K, V> toMap() {
if (size == 0) return emptyMap();
return toMutableMap();
}

@override
KMutableMap<K, V> toMutableMap() {
return mutableMapOf(map);
}

@override
String toString() {
return entries.joinToString(separator: ", ", prefix: "{", postfix: "}", transform: (it) => _entryToString(it));
Expand Down
14 changes: 13 additions & 1 deletion lib/src/k_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,17 @@ abstract class KMapExtension<K, V> {
*/
M mapValuesTo<R, M extends KMutableMap<K, R>>(M destination, R Function(KMapEntry<K, V> entry) transform);

// TODO toMutableMap
/**
* Returns a new read-only map containing all key-value pairs from the original map.
*
* The returned map preserves the entry iteration order of the original map.
*/
KMap<K, V> toMap();

/**
* Returns a new mutable map containing all key-value pairs from the original map.
*
* The returned map preserves the entry iteration order of the original map.
*/
KMutableMap<K, V> toMutableMap();
}
1 change: 0 additions & 1 deletion lib/src/k_map_mutable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ abstract class KMutableMapExtension<K, V> {
*/
void putAllPairs(KIterable<KPair<K, V>> pairs);

// TODO toMap
/**
* If the specified key is not already associated with a value (or is mapped to `null`) associates it with the given value and returns `null`, else returns the current value.
*
Expand Down
34 changes: 34 additions & 0 deletions test/collection/map_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,38 @@ void main() {
expect(mapped.size, 2);
});
});

group("toMap", () {
test("makes a copy which doesn't share memory", () {
final map = mutableMapOf({
1: "Bulbasaur",
2: "Ivysaur",
});
final copy = map.toMap();
expect(copy, map);
map.put(3, "Venusaur");
expect(map.size, 3);
expect(copy.size, 2);
});

test("make a copy of an empty list", () {
final map = emptyMap();
final copy = map.toMap();
expect(copy, map);
});
});

group("toMutableMap", () {
test("makes a copy", () {
var map = mapOf({
1: "Bulbasaur",
2: "Ivysaur",
});
final copy = map.toMutableMap();
expect(map, copy);
copy.put(3, "Venusaur");
expect(copy.size, 3);
expect(map.size, 2);
});
});
}