From bad5cb46d1658b830128b8b6f44f66be9fdf5f40 Mon Sep 17 00:00:00 2001 From: Vladislav Date: Fri, 13 Oct 2023 12:36:41 +0300 Subject: [PATCH 1/2] 1 --- package.json | 2 +- src/transformStateWithClones.js | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 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 96d4f7e7e..59c695fa3 100644 --- a/src/transformStateWithClones.js +++ b/src/transformStateWithClones.js @@ -7,7 +7,32 @@ * @return {Object[]} */ function transformStateWithClones(state, actions) { - // write code here + const result = []; + let currentState = { ...state }; + + for (const action of actions) { + const { type, extraData, keysToRemove } = action; + + let newState = { ...currentState }; + + if (type === 'addProperties') { + for (const key in extraData) { + newState[key] = extraData[key]; + } + } else if (type === 'removeProperties') { + for (const key of keysToRemove) { + delete newState[key]; + } + } else if (type === 'clear') { + newState = {}; + } + + result.push(newState); + + currentState = newState; + } + + return result; } module.exports = transformStateWithClones; From a5427e58529c39d4e7ce190a82dfe83a1c72aba5 Mon Sep 17 00:00:00 2001 From: Zava96 <103376060+Zava96@users.noreply.github.com> Date: Fri, 13 Oct 2023 18:19:53 +0300 Subject: [PATCH 2/2] Update transformStateWithClones.js --- src/transformStateWithClones.js | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/transformStateWithClones.js b/src/transformStateWithClones.js index 59c695fa3..98ebe54ec 100644 --- a/src/transformStateWithClones.js +++ b/src/transformStateWithClones.js @@ -13,26 +13,27 @@ function transformStateWithClones(state, actions) { for (const action of actions) { const { type, extraData, keysToRemove } = action; - let newState = { ...currentState }; - - if (type === 'addProperties') { - for (const key in extraData) { - newState[key] = extraData[key]; - } - } else if (type === 'removeProperties') { - for (const key of keysToRemove) { - delete newState[key]; - } - } else if (type === 'clear') { - newState = {}; + const newState = { ...currentState }; + + switch (type) { + case 'addProperties': + for (const key in extraData) { + newState[key] = extraData[key]; + } + break; + case 'removeProperties': + for (const key of keysToRemove) { + delete newState[key]; + } + break; + case 'clear': + Object.keys(newState).forEach((key) => delete newState[key]); + break; } - result.push(newState); + result.push({ ...newState }); - currentState = newState; } return result; } - -module.exports = transformStateWithClones;