diff --git a/lib/src/utils/map_copy_extension.dart b/lib/src/utils/map_copy_extension.dart index 7d2bbaec..9e3018b7 100644 --- a/lib/src/utils/map_copy_extension.dart +++ b/lib/src/utils/map_copy_extension.dart @@ -1,17 +1,17 @@ /* MIT License -* +* * Copyright (C) 2019, 2020, 2021 Famedly GmbH -* +* * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: -* +* * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. -* +* * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -22,16 +22,25 @@ */ extension MapCopyExtension on Map { + dynamic _copyValue(dynamic value) { + if (value is Map) { + return value.copy(); + } + if (value is List) { + final ret = []; + for (final val in value) { + ret.add(_copyValue(val)); + } + return ret; + } + return value; + } + /// Deep-copies a given json map Map copy() { final copy = Map.from(this); for (final entry in copy.entries) { - if (entry.value is Map) { - copy[entry.key] = (entry.value as Map).copy(); - } - if (entry.value is List) { - copy[entry.key] = List.from(entry.value); - } + copy[entry.key] = _copyValue(entry.value); } return copy; } diff --git a/test/map_copy_extension_test.dart b/test/map_copy_extension_test.dart index 38d9290a..329771d9 100644 --- a/test/map_copy_extension_test.dart +++ b/test/map_copy_extension_test.dart @@ -1,17 +1,17 @@ /* MIT License -* +* * Copyright (C) 2019, 2020, 2021 Famedly GmbH -* +* * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: -* +* * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. -* +* * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -40,5 +40,18 @@ void main() { original['child']['list'].add(3); expect(copy['child']['list'], [1, 2]); }); + test('should do arrays', () { + final original = { + 'arr': [ + [1, 2], + {'beep': 'boop'}, + ], + }; + final copy = original.copy(); + original['arr'][0].add(3); + expect(copy['arr'][0], [1, 2]); + original['arr'][1]['beep'] = 'blargh'; + expect(copy['arr'][1]['beep'], 'boop'); + }); }); }