From 78e3890e982358d965a21ecbd6fc2a860898d10e Mon Sep 17 00:00:00 2001 From: oh Date: Wed, 9 May 2018 20:01:55 +0100 Subject: [PATCH 1/4] fix(deepClone): Fixed problems with array values Would previously create an object with indices as keys. Now properly clones as array. #658 --- snippets/deepClone.md | 20 +++++++++++++++----- test/deepClone/deepClone.js | 8 ++++++++ test/deepClone/deepClone.test.js | 4 ++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/snippets/deepClone.md b/snippets/deepClone.md index fe2af84eb..7d9905c0a 100644 --- a/snippets/deepClone.md +++ b/snippets/deepClone.md @@ -8,15 +8,25 @@ Use `Object.keys()` and `Array.forEach()` to determine which key-value pairs nee ```js const deepClone = obj => { - let clone = Object.assign({}, obj); - Object.keys(clone).forEach( - key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key]) - ); - return clone; +if (Array.isArray(obj)){ + let arr = []; + obj.forEach( + (i,v) => (arr[i] = typeof v === 'object' ? deepClone(v) : v) + ) + return arr; +}else { + let clone = Object.assign({}, obj); + Object.keys(clone).forEach( + key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key]) + ); + return clone; +} }; ``` ```js + + const a = { foo: 'bar', obj: { a: 1, b: 2 } }; const b = deepClone(a); // a !== b, a.obj !== b.obj ``` diff --git a/test/deepClone/deepClone.js b/test/deepClone/deepClone.js index dcf7720b4..33d00a82c 100644 --- a/test/deepClone/deepClone.js +++ b/test/deepClone/deepClone.js @@ -1,8 +1,16 @@ const deepClone = obj => { +if (Array.isArray(obj)){ +let arr = []; +obj.forEach( +(i,v) => (arr[i] = typeof v === 'object' ? deepClone(v) : v) +) +return arr; +}else { let clone = Object.assign({}, obj); Object.keys(clone).forEach( key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key]) ); return clone; +} }; module.exports = deepClone; \ No newline at end of file diff --git a/test/deepClone/deepClone.test.js b/test/deepClone/deepClone.test.js index 03c936c46..579adb6d5 100644 --- a/test/deepClone/deepClone.test.js +++ b/test/deepClone/deepClone.test.js @@ -7,8 +7,12 @@ test('Testing deepClone', (t) => { t.true(typeof deepClone === 'function', 'deepClone is a Function'); const a = { foo: 'bar', obj: { a: 1, b: 2 } }; const b = deepClone(a); + const c = [{foo: 'bar'}]; + const d = deepClone(c); t.notEqual(a, b, 'Shallow cloning works'); t.notEqual(a.obj, b.obj, 'Deep cloning works'); + t.notEqual(c, d, 'Array shallow cloning works'); + t.notEqual(c[0], d[0], 'Array deep cloning works'); //t.deepEqual(deepClone(args..), 'Expected'); //t.equal(deepClone(args..), 'Expected'); //t.false(deepClone(args..), 'Expected'); From 76c56195b12c852eb130611a8046219a2d98106f Mon Sep 17 00:00:00 2001 From: Oscar Date: Thu, 10 May 2018 19:22:35 +0100 Subject: [PATCH 2/4] Simplify deepClone snippet's handling of arrays. --- snippets/deepClone.md | 12 +++--------- test/deepClone/deepClone.js | 10 +--------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/snippets/deepClone.md b/snippets/deepClone.md index 7d9905c0a..e7707918f 100644 --- a/snippets/deepClone.md +++ b/snippets/deepClone.md @@ -7,26 +7,20 @@ Use `Object.assign()` and an empty object (`{}`) to create a shallow clone of th Use `Object.keys()` and `Array.forEach()` to determine which key-value pairs need to be deep cloned. ```js + const deepClone = obj => { -if (Array.isArray(obj)){ - let arr = []; - obj.forEach( - (i,v) => (arr[i] = typeof v === 'object' ? deepClone(v) : v) - ) - return arr; -}else { let clone = Object.assign({}, obj); Object.keys(clone).forEach( key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key]) ); - return clone; -} + return Array.isArray(obj) ? (clone.length = obj.length) && Array.from(clone) : obj; }; ``` ```js + const a = { foo: 'bar', obj: { a: 1, b: 2 } }; const b = deepClone(a); // a !== b, a.obj !== b.obj ``` diff --git a/test/deepClone/deepClone.js b/test/deepClone/deepClone.js index 33d00a82c..f613e5660 100644 --- a/test/deepClone/deepClone.js +++ b/test/deepClone/deepClone.js @@ -1,16 +1,8 @@ const deepClone = obj => { -if (Array.isArray(obj)){ -let arr = []; -obj.forEach( -(i,v) => (arr[i] = typeof v === 'object' ? deepClone(v) : v) -) -return arr; -}else { let clone = Object.assign({}, obj); Object.keys(clone).forEach( key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key]) ); -return clone; -} +return Array.isArray(obj) ? (clone.length = obj.length) && Array.from(clone) : obj; }; module.exports = deepClone; \ No newline at end of file From 1f7377e7d0955d9600a9ce4a8ccdf9ec8d51c98c Mon Sep 17 00:00:00 2001 From: Oscar Date: Fri, 11 May 2018 10:23:59 +0100 Subject: [PATCH 3/4] deepClone: Fixed indents & failing tests --- snippets/deepClone.md | 12 +++++++----- test/deepClone/deepClone.js | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/snippets/deepClone.md b/snippets/deepClone.md index e7707918f..de791ee7e 100644 --- a/snippets/deepClone.md +++ b/snippets/deepClone.md @@ -8,12 +8,13 @@ Use `Object.keys()` and `Array.forEach()` to determine which key-value pairs nee ```js + const deepClone = obj => { - let clone = Object.assign({}, obj); - Object.keys(clone).forEach( - key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key]) - ); - return Array.isArray(obj) ? (clone.length = obj.length) && Array.from(clone) : obj; + let clone = Object.assign({}, obj); + Object.keys(clone).forEach( + key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key]) + ); + return Array.isArray(obj) ? Array.from(clone) : clone; }; ``` @@ -21,6 +22,7 @@ const deepClone = obj => { + const a = { foo: 'bar', obj: { a: 1, b: 2 } }; const b = deepClone(a); // a !== b, a.obj !== b.obj ``` diff --git a/test/deepClone/deepClone.js b/test/deepClone/deepClone.js index f613e5660..62e3364ff 100644 --- a/test/deepClone/deepClone.js +++ b/test/deepClone/deepClone.js @@ -3,6 +3,6 @@ let clone = Object.assign({}, obj); Object.keys(clone).forEach( key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key]) ); -return Array.isArray(obj) ? (clone.length = obj.length) && Array.from(clone) : obj; +return Array.isArray(obj) ? Array.from(clone) : clone; }; -module.exports = deepClone; \ No newline at end of file +module.exports = deepClone; From 3f455bd10016e2cbedac25a88a6859f09406436e Mon Sep 17 00:00:00 2001 From: Oscar Date: Fri, 11 May 2018 10:25:23 +0100 Subject: [PATCH 4/4] Fix inconsistent quotes --- test/deepClone/deepClone.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/deepClone/deepClone.test.js b/test/deepClone/deepClone.test.js index 579adb6d5..c2276f6ae 100644 --- a/test/deepClone/deepClone.test.js +++ b/test/deepClone/deepClone.test.js @@ -7,12 +7,12 @@ test('Testing deepClone', (t) => { t.true(typeof deepClone === 'function', 'deepClone is a Function'); const a = { foo: 'bar', obj: { a: 1, b: 2 } }; const b = deepClone(a); - const c = [{foo: 'bar'}]; + const c = [{foo: "bar"}]; const d = deepClone(c); t.notEqual(a, b, 'Shallow cloning works'); t.notEqual(a.obj, b.obj, 'Deep cloning works'); - t.notEqual(c, d, 'Array shallow cloning works'); - t.notEqual(c[0], d[0], 'Array deep cloning works'); + t.notEqual(c, d, "Array shallow cloning works"); + t.notEqual(c[0], d[0], "Array deep cloning works"); //t.deepEqual(deepClone(args..), 'Expected'); //t.equal(deepClone(args..), 'Expected'); //t.false(deepClone(args..), 'Expected');