Skip to content

Commit

Permalink
Deep clone map and set (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
angus-c authored Jun 4, 2022
1 parent 55f54af commit 40b46af
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/collection-clone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ function clone(obj) {
let result = obj;
var type = {}.toString.call(obj).slice(8, -1);
if (type == 'Set') {
return new Set(obj);
return new Set([...obj].map(value => clone(value)));
}
if (type == 'Map') {
return new Map(obj);
return new Map([...obj].map(kv => [clone(kv[0]), clone(kv[1])]));
}
if (type == 'Date') {
return new Date(obj.getTime());
Expand Down
38 changes: 37 additions & 1 deletion test/collection-clone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ test('clones Dates and RegExps', function(t) {
});

test('clones Sets and Maps', function(t) {
t.plan(14);
t.plan(24);

// as root objects
var set = new Set(['a', 'b', 'c', 'a']);
Expand All @@ -88,6 +88,42 @@ test('clones Sets and Maps', function(t) {
t.deepEqual(Array.from(map.entries()).length, 4);
t.deepEqual(Array.from(mapClone.entries()).length, 3);

// as root set object with nested set and map
var subset1 = new Set(['a', 'b', 'c', 'a']);
var submap1 = new Map();
var keys1 = [{a: 1}, {b: 2}, {c: 3}];
keys1.forEach((key, i) => submap1.set(key, i));

var set1 = new Set(['a', submap1, subset1, 'a']);
var set1Clone = clone(set1);
t.deepEqual(set1.entries(), set1Clone.entries());
subset1.add('d');
t.deepEqual(Array.from(Array.from(set1)[2].entries()).length, 4);
t.deepEqual(Array.from(Array.from(set1Clone)[2].entries()).length, 3);
submap1.set({d: 4}, 3);
submap1.set({e: 5}, 4);
t.deepEqual(Array.from(Array.from(set1)[1].entries()).length, 5);
t.deepEqual(Array.from(Array.from(set1Clone)[1].entries()).length, 3);

// as root map object with nested set and map
var subset2 = new Set(['f', 'f', 'e', 'a']);
var submap2 = new Map();
var keys2 = [{aa: 1}, {bb: 2}, {cc: 3}];
keys2.forEach((key, i) => submap2.set(key, i));

var map1 = new Map();
map1.set({map: true}, submap2);
map1.set({set: true}, subset2);
var map1Clone = clone(map1);
t.deepEqual(map1.entries(), map1Clone.entries());
subset2.add('gg');
subset2.add('hh');
t.deepEqual(Array.from(Array.from(map1.entries())[1][1]).length, 5);
t.deepEqual(Array.from(Array.from(map1Clone.entries())[1][1]).length, 3);
submap2.set({dd: 4}, 3);
t.deepEqual(Array.from(Array.from(map1.entries())[0][1]).length, 4);
t.deepEqual(Array.from(Array.from(map1Clone.entries())[0][1]).length, 3);

// as properties
var objToString = Object.prototype.toString;
var set2 = new Set(['ant', 'bee', 'dragonfly', 'bee']);
Expand Down

0 comments on commit 40b46af

Please sign in to comment.