You can call me lety. π€ͺ
letyletylety / deepcopy Goto Github PK
View Code? Open in Web Editor NEWrecursive deep copy for nested collections
Home Page: https://pub.dev/packages/deepcopy
License: BSD 2-Clause "Simplified" License
recursive deep copy for nested collections
Home Page: https://pub.dev/packages/deepcopy
License: BSD 2-Clause "Simplified" License
I would like to see generic support added to the library. Here is an example implementation:
class ClonerService {
static List<T> deepcopyList<T>(List<T> list) {
List<T> copy = [];
for (final item in list) {
if (item is Map) {
copy.add(deepcopyMap(item) as T);
} else if (item is List) {
copy.add(deepcopyList(item) as T);
} else if (item is Set) {
copy.add(deepcopySet(item) as T);
} else {
copy.add(item);
}
}
return copy;
}
static Map<TKey, TValue> deepcopyMap<TKey, TValue>(Map<TKey, TValue> map) {
Map<TKey, TValue> copy = {};
for (final entry in map.entries) {
final key = entry.key;
final value = entry.value;
if (value is Map) {
copy[key] = deepcopyMap(value) as TValue;
} else if (value is List) {
copy[key] = deepcopyList(value) as TValue;
} else if (value is Set) {
copy[key] = deepcopySet(value) as TValue;
} else {
copy[key] = value;
}
}
return copy;
}
static Set<T> deepcopySet<T>(Set<T> set) {
Set<T> copy = {};
for (final item in set) {
if (item is Map) {
copy.add(deepcopyMap(item) as T);
} else if (item is List) {
copy.add(deepcopyList(item) as T);
} else if (item is Set) {
copy.add(deepcopySet(item) as T);
} else {
copy.add(item);
}
}
return copy;
}
}
extension MapExtensions<TKey, TValue> on Map<TKey, TValue> {
Map<TKey, TValue> deepcopy() {
return ClonerService.deepcopyMap<TKey, TValue>(this);
}
}
extension ListExtensions<TKey, T> on List<T> {
List<T> deepcopy() {
return ClonerService.deepcopyList<T>(this);
}
}
extension SetExtensions<T> on Set<T> {
Set<T> deepcopy() {
return ClonerService.deepcopySet<T>(this);
}
}
Tests:
void main() {
group('cloner', () {
testWidgets('Map<int, Map>', (tester) async {
final Map<int, Map> original = {
1: {
'abc': {'abc': 'abc'}
}
};
final copy = original.deepcopy();
expect(original.values.first.entries.first != copy.values.first.entries.first,
true);
});
testWidgets('List<Map>', (tester) async {
final List<Map> original = [
{
'abc': {'abc': 'abc'}
}
];
final copy = original.deepcopy();
expect(original.first.entries.first != copy.first.entries.first, true);
});
testWidgets('Set<Map>', (tester) async {
final Set<Map> original = {
{
'abc': {'abc': 'abc'}
}
};
final copy = original.deepcopy();
expect(original.first.entries.first != copy.first.entries.first, true);
});
});
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.