From 8a17c900a5c80458c736c743ea4cf36a71c29ffa Mon Sep 17 00:00:00 2001 From: JK Date: Sat, 13 Mar 2021 04:56:53 +0000 Subject: [PATCH 1/4] Add deepcloning for sets and maps. Adds deepcloning functionality for Set and Map classes. Related to: #17 Signed-off-by: JK --- index.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/index.js b/index.js index 98b42f6..3b438a4 100644 --- a/index.js +++ b/index.js @@ -38,6 +38,8 @@ function rfdc (opts) { if (typeof o !== 'object' || o === null) return o if (o instanceof Date) return new Date(o) if (Array.isArray(o)) return cloneArray(o, clone) + if (o instanceof Map) return new Map(cloneArray(Array.from(o), clone)) + if (o instanceof Set) return new Set(cloneArray(Array.from(o), clone)) var o2 = {} for (var k in o) { if (Object.hasOwnProperty.call(o, k) === false) continue @@ -46,6 +48,10 @@ function rfdc (opts) { o2[k] = cur } else if (cur instanceof Date) { o2[k] = new Date(cur) + } else if (cur instanceof Map) { + o2[k] = new Map(cloneArray(Array.from(cur), clone)) + } else if (cur instanceof Set) { + o2[k] = new Set(cloneArray(Array.from(cur), clone)) } else if (ArrayBuffer.isView(cur)) { o2[k] = copyBuffer(cur) } else { @@ -59,6 +65,8 @@ function rfdc (opts) { if (typeof o !== 'object' || o === null) return o if (o instanceof Date) return new Date(o) if (Array.isArray(o)) return cloneArray(o, cloneProto) + if (o instanceof Map) return new Map(cloneArray(Array.from(o), cloneProto)) + if (o instanceof Set) return new Set(cloneArray(Array.from(o), cloneProto)) var o2 = {} for (var k in o) { var cur = o[k] @@ -66,6 +74,10 @@ function rfdc (opts) { o2[k] = cur } else if (cur instanceof Date) { o2[k] = new Date(cur) + } else if (cur instanceof Map) { + o2[k] = new Map(cloneArray(Array.from(cur), cloneProto)) + } else if (cur instanceof Set) { + o2[k] = new Set(cloneArray(Array.from(cur), cloneProto)) } else if (ArrayBuffer.isView(cur)) { o2[k] = copyBuffer(cur) } else { @@ -110,6 +122,8 @@ function rfdcCircles (opts) { if (typeof o !== 'object' || o === null) return o if (o instanceof Date) return new Date(o) if (Array.isArray(o)) return cloneArray(o, clone) + if (o instanceof Map) return new Map(cloneArray(Array.from(o), clone)) + if (o instanceof Set) return new Set(cloneArray(Array.from(o), clone)) var o2 = {} refs.push(o) refsNew.push(o2) @@ -120,6 +134,10 @@ function rfdcCircles (opts) { o2[k] = cur } else if (cur instanceof Date) { o2[k] = new Date(cur) + } else if (cur instanceof Map) { + o2[k] = new Map(cloneArray(Array.from(cur), clone)) + } else if (cur instanceof Set) { + o2[k] = new Set(cloneArray(Array.from(cur), clone)) } else if (ArrayBuffer.isView(cur)) { o2[k] = copyBuffer(cur) } else { @@ -140,6 +158,8 @@ function rfdcCircles (opts) { if (typeof o !== 'object' || o === null) return o if (o instanceof Date) return new Date(o) if (Array.isArray(o)) return cloneArray(o, cloneProto) + if (o instanceof Map) return new Map(cloneArray(Array.from(o), cloneProto)) + if (o instanceof Set) return new Set(cloneArray(Array.from(o), cloneProto)) var o2 = {} refs.push(o) refsNew.push(o2) @@ -149,6 +169,10 @@ function rfdcCircles (opts) { o2[k] = cur } else if (cur instanceof Date) { o2[k] = new Date(cur) + } else if (cur instanceof Map) { + o2[k] = new Map(cloneArray(Array.from(cur), cloneProto)) + } else if (cur instanceof Set) { + o2[k] = new Set(cloneArray(Array.from(cur), cloneProto)) } else if (ArrayBuffer.isView(cur)) { o2[k] = copyBuffer(cur) } else { From cdd179dcfb7f341988fd0dc51e57d5cde33e76aa Mon Sep 17 00:00:00 2001 From: JK Date: Sat, 13 Mar 2021 22:29:36 +0000 Subject: [PATCH 2/4] Add tests for sets and maps. Adds standalone and nested tests for sets and maps. Related to: #17 Signed-off-by: JK --- test/index.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/index.js b/test/index.js index 19b2663..6a74cba 100644 --- a/test/index.js +++ b/test/index.js @@ -265,4 +265,24 @@ function types (clone, label) { deepEqual(Array.from(cloned.view2), [input2, input3], 'cloned value content is correct') deepEqual(Array.from(cloned.view3), [input1, 0, input2, input3], 'cloned value content is correct') }) + test(`${label} - maps`, async ({ same, isNot }) => { + const map = new Map([['a', 1]]) + same(Array.from(clone(map)), [['a', 1]]) + isNot(clone(map), map) + }) + test(`${label} - sets`, async ({ same, isNot }) => { + const set = new Set([1]) + same(Array.from(clone(set)), [1]) + isNot(clone(set), set) + }) + test(`${label} - nested maps`, async ({ same, isNot }) => { + const data = { m: new Map([['a', 1]]) } + same(Array.from(clone(data).m), [['a', 1]]) + isNot(clone(data).m, data.m) + }) + test(`${label} - nested sets`, async ({ same, isNot }) => { + const data = { s: new Set([1]) } + same(Array.from(clone(data).s), [1]) + isNot(clone(data).s, data.s) + }) } From 6e3d1939c3768e0b1828c234831ef5e12e3f3afb Mon Sep 17 00:00:00 2001 From: JK Date: Sat, 13 Mar 2021 22:33:48 +0000 Subject: [PATCH 3/4] Add messages for Set and Map tests. Adds 'same value' and 'different object' tests for sets and maps. Signed-off-by: JK --- test/index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/index.js b/test/index.js index 6a74cba..0e389e4 100644 --- a/test/index.js +++ b/test/index.js @@ -267,22 +267,22 @@ function types (clone, label) { }) test(`${label} - maps`, async ({ same, isNot }) => { const map = new Map([['a', 1]]) - same(Array.from(clone(map)), [['a', 1]]) - isNot(clone(map), map) + same(Array.from(clone(map)), [['a', 1]], "same value") + isNot(clone(map), map, "different object") }) test(`${label} - sets`, async ({ same, isNot }) => { const set = new Set([1]) same(Array.from(clone(set)), [1]) - isNot(clone(set), set) + isNot(clone(set), set, "different object") }) test(`${label} - nested maps`, async ({ same, isNot }) => { const data = { m: new Map([['a', 1]]) } - same(Array.from(clone(data).m), [['a', 1]]) - isNot(clone(data).m, data.m) + same(Array.from(clone(data).m), [['a', 1]], "same value") + isNot(clone(data).m, data.m, "different object") }) test(`${label} - nested sets`, async ({ same, isNot }) => { const data = { s: new Set([1]) } - same(Array.from(clone(data).s), [1]) - isNot(clone(data).s, data.s) + same(Array.from(clone(data).s), [1], "same value") + isNot(clone(data).s, data.s, "different object") }) } From 4b284f2b8ef1346e0aa1e45071ff69381c97e6ea Mon Sep 17 00:00:00 2001 From: JK Date: Sat, 13 Mar 2021 22:42:07 +0000 Subject: [PATCH 4/4] Fit to standard style. Signed-off-by: JK --- test/index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/index.js b/test/index.js index 0e389e4..458712f 100644 --- a/test/index.js +++ b/test/index.js @@ -267,22 +267,22 @@ function types (clone, label) { }) test(`${label} - maps`, async ({ same, isNot }) => { const map = new Map([['a', 1]]) - same(Array.from(clone(map)), [['a', 1]], "same value") - isNot(clone(map), map, "different object") + same(Array.from(clone(map)), [['a', 1]], 'same value') + isNot(clone(map), map, 'different object') }) test(`${label} - sets`, async ({ same, isNot }) => { const set = new Set([1]) same(Array.from(clone(set)), [1]) - isNot(clone(set), set, "different object") + isNot(clone(set), set, 'different object') }) test(`${label} - nested maps`, async ({ same, isNot }) => { const data = { m: new Map([['a', 1]]) } - same(Array.from(clone(data).m), [['a', 1]], "same value") - isNot(clone(data).m, data.m, "different object") + same(Array.from(clone(data).m), [['a', 1]], 'same value') + isNot(clone(data).m, data.m, 'different object') }) test(`${label} - nested sets`, async ({ same, isNot }) => { const data = { s: new Set([1]) } - same(Array.from(clone(data).s), [1], "same value") - isNot(clone(data).s, data.s, "different object") + same(Array.from(clone(data).s), [1], 'same value') + isNot(clone(data).s, data.s, 'different object') }) }