From 5d04d34eb67a4bb4bcd8601966aa5a40a05ca5ee Mon Sep 17 00:00:00 2001 From: Joshua Skootsky Date: Sat, 2 May 2020 23:47:02 -0400 Subject: [PATCH 1/3] test(deepCopy): added a test for copying an object with arrays inside it --- src/deepCopy.js | 2 +- src/tests/deepCopy.test.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/deepCopy.js b/src/deepCopy.js index 4fa8cb5..5d36450 100644 --- a/src/deepCopy.js +++ b/src/deepCopy.js @@ -5,7 +5,7 @@ * @return {(Object | Array)} - The new, cloned object or array */ const deepCopy = (o) => { - return JSON.parse(JSON.stringify(o)); + return JSON.parse(JSON.stringify(o)); }; module.exports = deepCopy; diff --git a/src/tests/deepCopy.test.js b/src/tests/deepCopy.test.js index 74f923a..1f7405d 100644 --- a/src/tests/deepCopy.test.js +++ b/src/tests/deepCopy.test.js @@ -18,4 +18,11 @@ describe("deepCopy", () => { expect(newObj.name.first).to.equal("Alpha"); expect(myObj.name.first).to.equal("Bravo"); }); + it("can copy an object that has arrays as values", () => { + const myObj = { + a: [1], + } + const newObj = deepCopy(myObj); + expect(newObj.a).to.deep.equal(myObj.a); + }); }); From 91e9abf7122c873c585debf12140f48f44360db9 Mon Sep 17 00:00:00 2001 From: Joshua Skootsky Date: Mon, 4 May 2020 01:32:17 -0400 Subject: [PATCH 2/3] feat(deepCopy): uses v8 serialize Much better and richer than using JSON stringify! --- src/tests/deepCopy.test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/tests/deepCopy.test.js b/src/tests/deepCopy.test.js index 1f7405d..9dd5d2b 100644 --- a/src/tests/deepCopy.test.js +++ b/src/tests/deepCopy.test.js @@ -25,4 +25,20 @@ describe("deepCopy", () => { const newObj = deepCopy(myObj); expect(newObj.a).to.deep.equal(myObj.a); }); + it("can copy an object that has undefined, NaN, Infinity, and functions as values", () => { + const myObj = { + a: [1], + b: NaN, + c: function() { + return "Hello!"; + }, + d: Infinity, + } + const newObj = deepCopy(myObj); + expect(newObj.a).to.deep.equal(myObj.a); + expect(newObj.b).to.equal(myObj.b); // AssertionError: expected null to equal NaN + expect(newObj.c).to.equal(myObj.c); + expect(newObj.d).to.equal(myObj.d); + }); + }); From a0121d95b9ca10b25ae02260d5d3c0cbc54a6941 Mon Sep 17 00:00:00 2001 From: Joshua Skootsky Date: Mon, 4 May 2020 01:33:05 -0400 Subject: [PATCH 3/3] feature(deepCopy): uses v8 serialize v8.deserialize(v8.serialize(o)) can handle things that JSON stringify and destringify cannot. Todo is being able to copy over objects with functions as properties. And cyclic graphs. --- src/deepCopy.js | 4 +++- src/tests/deepCopy.test.js | 16 ++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/deepCopy.js b/src/deepCopy.js index 5d36450..33f6a83 100644 --- a/src/deepCopy.js +++ b/src/deepCopy.js @@ -1,3 +1,5 @@ +const v8 = require('v8'); // Node v11 and onwards + /** * A function to deeply copy an object * (circular references will break it). @@ -5,7 +7,7 @@ * @return {(Object | Array)} - The new, cloned object or array */ const deepCopy = (o) => { - return JSON.parse(JSON.stringify(o)); + return v8.deserialize(v8.serialize(o)); }; module.exports = deepCopy; diff --git a/src/tests/deepCopy.test.js b/src/tests/deepCopy.test.js index 9dd5d2b..7ac35c3 100644 --- a/src/tests/deepCopy.test.js +++ b/src/tests/deepCopy.test.js @@ -25,20 +25,24 @@ describe("deepCopy", () => { const newObj = deepCopy(myObj); expect(newObj.a).to.deep.equal(myObj.a); }); - it("can copy an object that has undefined, NaN, Infinity, and functions as values", () => { + it("can copy an object that has undefined, NaN, Infinity as values", () => { const myObj = { a: [1], - b: NaN, - c: function() { - return "Hello!"; - }, + b: new Date(), + c: undefined, d: Infinity, + e: NaN, + f: false, } const newObj = deepCopy(myObj); expect(newObj.a).to.deep.equal(myObj.a); - expect(newObj.b).to.equal(myObj.b); // AssertionError: expected null to equal NaN + expect(newObj.b).to.deep.equal(myObj.b); expect(newObj.c).to.equal(myObj.c); expect(newObj.d).to.equal(myObj.d); + expect(newObj.e).to.not.equal(newObj.e); // test for NaN being copied over + // > NaN !== NaN + //true + expect(newObj.f).to.equal(myObj.f); }); });