-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(map_extension): revert Map copy in
fillEmptyKeyValues
(#121)
* fix(map_extension): revert Map copy in `fillEmptyKeyValues` * test(map_extension): adapt tests for mutation
- Loading branch information
1 parent
d7cca76
commit c015429
Showing
5 changed files
with
56 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,25 @@ | ||
/// Map extension. | ||
extension MapExtension<K, V> on Map<K, V> { | ||
/// Returns a new [Map] filled with the result of [ifAbsent] for [keys] that | ||
/// are not present in this [Map]. | ||
/// Fills this [Map] with the result of [ifAbsent] for [keys] that are not | ||
/// present. | ||
/// | ||
/// Unlike [Map.update], this method does not mutate this [Map]. | ||
/// Like [Map.update], this method mutates this [Map]. | ||
/// | ||
/// Example: | ||
/// ```dart | ||
/// const map = <int, bool?>{1: true, 2: false}; | ||
/// final filled = map.fillEmptyKeyValues( | ||
/// keys: const [0, 1, 2, 5], | ||
/// ifAbsent: () => null, | ||
/// ); | ||
/// assert(filled == const {0: null, 1: true, 2: false, 5: null}); | ||
/// final map = <int, bool?>{1: true, 2: false} | ||
/// ..fillEmptyKeyValues( | ||
/// keys: const [0, 1, 2, 5], | ||
/// ifAbsent: () => null, | ||
/// ); | ||
/// assert(map == const {0: null, 1: true, 2: false, 5: null}); | ||
/// ``` | ||
Map<K, V> fillEmptyKeyValues({ | ||
void fillEmptyKeyValues({ | ||
required Iterable<K> keys, | ||
required V Function() ifAbsent, | ||
}) { | ||
final map = Map<K, V>.of(this); | ||
|
||
for (final key in keys) { | ||
map.update(key, (value) => value, ifAbsent: ifAbsent); | ||
update(key, (value) => value, ifAbsent: ifAbsent); | ||
} | ||
|
||
return map; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters