Skip to content
This repository was archived by the owner on Oct 12, 2024. It is now read-only.

Commit 7b5faf8

Browse files
committed
Update
1 parent 2577bb6 commit 7b5faf8

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

lib/src/collections/src/merge.dart

+28-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import 'dart:collection' show Queue;
1414

1515
import 'package:collection/collection.dart' show mergeMaps;
1616

17+
import '../../../xyz_utils.dart';
18+
1719
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
1820

1921
/// Merges two Iterables into one. Supported Iterable types are List, Set, and
@@ -88,15 +90,36 @@ dynamic mergeDataDeep(
8890
return elseFilter?.call(b) ?? b;
8991
}
9092

91-
/// Merges all [maps] deeply.
92-
Map mergeMapsDeep(List<Map> maps) {
93-
var merged = {};
94-
for (final map in maps) {
95-
merged = mergeDataDeep(merged, map);
93+
/// Merges all [jsons].
94+
Map<String, dynamic> mergeJson(List<Map<String, dynamic>> jsons) {
95+
var merged = <String, dynamic>{};
96+
for (final map in jsons) {
97+
final temp = letMap<String, dynamic>(_mergeJson(merged, map));
98+
if (temp != null) {
99+
merged = temp;
100+
}
96101
}
97102
return merged;
98103
}
99104

105+
Map<String, dynamic> _mergeJson(Map<String, dynamic> map1, Map<String, dynamic> map2) {
106+
final result = Map<String, dynamic>.from(map1);
107+
108+
map2.forEach((key, value) {
109+
if (result.containsKey(key)) {
110+
if (value is Map<String, dynamic> && result[key] is Map<String, dynamic>) {
111+
result[key] = _mergeJson(result[key], value);
112+
} else {
113+
result[key] = value;
114+
}
115+
} else {
116+
result[key] = value;
117+
}
118+
});
119+
120+
return result;
121+
}
122+
100123
/// Merges two data structures deeply and tries to perform toJson on objects.
101124
dynamic mergeDataDeepIncludeCallsToJson(dynamic a, dynamic b) {
102125
return mergeDataDeep(a, b, tryToJson);

0 commit comments

Comments
 (0)