2021-03-18 23:10:19 +00:00
|
|
|
/// Exposes the [mergeMap] function, which... merges Maps.
|
2021-05-14 08:06:16 +00:00
|
|
|
library angel3_merge_map;
|
2021-03-18 23:10:19 +00:00
|
|
|
|
2021-05-18 13:27:19 +00:00
|
|
|
dynamic _copyValues<K, V>(
|
2021-03-18 23:10:19 +00:00
|
|
|
Map<K, V> from, Map<K, V?>? to, bool recursive, bool acceptNull) {
|
|
|
|
for (var key in from.keys) {
|
|
|
|
if (from[key] is Map<K, V> && recursive) {
|
|
|
|
if (!(to![key] is Map<K, V>)) {
|
|
|
|
to[key] = <K, V>{} as V;
|
|
|
|
}
|
|
|
|
_copyValues(from[key] as Map, to[key] as Map?, recursive, acceptNull);
|
|
|
|
} else {
|
|
|
|
if (from[key] != null || acceptNull) to![key] = from[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Merges the values of the given maps together.
|
|
|
|
///
|
|
|
|
/// `recursive` is set to `true` by default. If set to `true`,
|
|
|
|
/// then nested maps will also be merged. Otherwise, nested maps
|
|
|
|
/// will overwrite others.
|
|
|
|
///
|
|
|
|
/// `acceptNull` is set to `false` by default. If set to `false`,
|
|
|
|
/// then if the value on a map is `null`, it will be ignored, and
|
|
|
|
/// that `null` will not be copied.
|
|
|
|
Map<K, V> mergeMap<K, V>(Iterable<Map<K, V>> maps,
|
2021-05-18 13:27:19 +00:00
|
|
|
{bool recursive = true, bool acceptNull = false}) {
|
|
|
|
var result = <K, V>{};
|
2021-03-18 23:10:19 +00:00
|
|
|
maps.forEach((Map<K, V> map) {
|
2021-05-14 08:06:16 +00:00
|
|
|
_copyValues(map, result, recursive, acceptNull);
|
2021-03-18 23:10:19 +00:00
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|