From 94490fd44318d9c4565859dd8d43b642beee48f3 Mon Sep 17 00:00:00 2001 From: ErnestMk1 Date: Tue, 3 Oct 2023 22:36:52 +0300 Subject: [PATCH 1/3] implemented task --- src/transformStateWithClones.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/transformStateWithClones.js b/src/transformStateWithClones.js index 96d4f7e7e..cdf44784a 100644 --- a/src/transformStateWithClones.js +++ b/src/transformStateWithClones.js @@ -7,7 +7,30 @@ * @return {Object[]} */ function transformStateWithClones(state, actions) { - // write code here + const stateClone = { ...state }; + const stateVersions = []; + + for (const object of actions) { + if (object.type === 'addProperties') { + Object.assign(stateClone, object.extraData); + + stateVersions.push({ ...stateClone }); + } else if (object.type === 'removeProperties') { + for (const toRemove of object.keysToRemove) { + delete stateClone[toRemove]; + } + + stateVersions.push({ ...stateClone }); + } else if (object.type === 'clear') { + for (const key in stateClone) { + delete stateClone[key]; + } + + stateVersions.push({ ...stateClone }); + } + } + + return stateVersions; } module.exports = transformStateWithClones; From fd0d53bdc242cba0b219fd40d80e1d583fc92f7f Mon Sep 17 00:00:00 2001 From: ErnestMk1 Date: Wed, 4 Oct 2023 21:44:51 +0300 Subject: [PATCH 2/3] made a destructuring. Replaced if/else with switch/case --- package.json | 2 +- src/transformStateWithClones.js | 37 +++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 2452d97a9..42a7994fe 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "license": "GPL-3.0", "devDependencies": { "@mate-academy/eslint-config": "*", - "@mate-academy/scripts": "^0.3.5", + "@mate-academy/scripts": "^1.2.8", "eslint": "^5.16.0", "eslint-plugin-jest": "^22.4.1", "eslint-plugin-node": "^8.0.1", diff --git a/src/transformStateWithClones.js b/src/transformStateWithClones.js index cdf44784a..e8a172409 100644 --- a/src/transformStateWithClones.js +++ b/src/transformStateWithClones.js @@ -11,22 +11,27 @@ function transformStateWithClones(state, actions) { const stateVersions = []; for (const object of actions) { - if (object.type === 'addProperties') { - Object.assign(stateClone, object.extraData); - - stateVersions.push({ ...stateClone }); - } else if (object.type === 'removeProperties') { - for (const toRemove of object.keysToRemove) { - delete stateClone[toRemove]; - } - - stateVersions.push({ ...stateClone }); - } else if (object.type === 'clear') { - for (const key in stateClone) { - delete stateClone[key]; - } - - stateVersions.push({ ...stateClone }); + const { type, extraData, keysToRemove } = object; + + switch (type) { + case 'addProperties': + Object.assign(stateClone, extraData); + + stateVersions.push({ ...stateClone }); + break; + case 'removeProperties': + for (const toRemove of keysToRemove) { + delete stateClone[toRemove]; + } + + stateVersions.push({ ...stateClone }); + break; + case 'clear': + for (const key in stateClone) { + delete stateClone[key]; + } + + stateVersions.push({ ...stateClone }); } } From 7f9fae2580cdad7518920572e5e32937877d36d9 Mon Sep 17 00:00:00 2001 From: ErnestMk1 Date: Thu, 5 Oct 2023 14:11:32 +0300 Subject: [PATCH 3/3] made saving of a state only once in the end of the loop --- src/transformStateWithClones.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/transformStateWithClones.js b/src/transformStateWithClones.js index e8a172409..7f1156c4d 100644 --- a/src/transformStateWithClones.js +++ b/src/transformStateWithClones.js @@ -7,7 +7,7 @@ * @return {Object[]} */ function transformStateWithClones(state, actions) { - const stateClone = { ...state }; + let stateClone = { ...state }; const stateVersions = []; for (const object of actions) { @@ -17,22 +17,18 @@ function transformStateWithClones(state, actions) { case 'addProperties': Object.assign(stateClone, extraData); - stateVersions.push({ ...stateClone }); break; case 'removeProperties': for (const toRemove of keysToRemove) { delete stateClone[toRemove]; } - stateVersions.push({ ...stateClone }); break; case 'clear': - for (const key in stateClone) { - delete stateClone[key]; - } - - stateVersions.push({ ...stateClone }); + stateClone = {}; } + + stateVersions.push({ ...stateClone }); } return stateVersions;