Skip to content

Commit

Permalink
Merge pull request #7 from passsy/feature/toMap_toMutableMap
Browse files Browse the repository at this point in the history
KMap.toMap, KMap.toMutableMap
  • Loading branch information
passsy authored Dec 2, 2018
2 parents aebe09c + 1a1ce38 commit 336d9cf
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
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);
});
});
}

0 comments on commit 336d9cf

Please sign in to comment.