From c6379585708253a518fc404a5fa459f3c995db2c Mon Sep 17 00:00:00 2001 From: Kseniia Chepur Date: Tue, 3 Oct 2023 18:29:07 +0300 Subject: [PATCH 1/4] implement solution --- src/transformStateWithClones.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/transformStateWithClones.js b/src/transformStateWithClones.js index 96d4f7e7e..b2207bc07 100644 --- a/src/transformStateWithClones.js +++ b/src/transformStateWithClones.js @@ -7,7 +7,36 @@ * @return {Object[]} */ function transformStateWithClones(state, actions) { - // write code here + const statesArray = []; + + for (let i = 0; i < actions.length; i++) { + if (actions[i].type === 'addProperties') { + const currentState = i ? { ...statesArray[i - 1] } : { ...state }; + + Object.assign(currentState, actions[i].extraData); + statesArray.push(currentState); + } + + if (actions[i].type === 'removeProperties') { + const currentState = i ? { ...statesArray[i - 1] } : { ...state }; + + for (const key of actions[i].keysToRemove) { + delete currentState[key]; + } + statesArray.push(currentState); + } + + if (actions[i].type === 'clear') { + const currentState = i ? { ...statesArray[i - 1] } : { ...state }; + + for (const key of Object.keys(currentState)) { + delete currentState[key]; + } + statesArray.push(currentState); + } + } + + return statesArray; } module.exports = transformStateWithClones; From 879f3ef0358d383f000421deb51824c5dae7b32a Mon Sep 17 00:00:00 2001 From: Kseniia Chepur Date: Tue, 3 Oct 2023 19:20:04 +0300 Subject: [PATCH 2/4] change to switch statement --- src/transformStateWithClones.js | 39 +++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/transformStateWithClones.js b/src/transformStateWithClones.js index b2207bc07..ee79249b0 100644 --- a/src/transformStateWithClones.js +++ b/src/transformStateWithClones.js @@ -10,29 +10,36 @@ function transformStateWithClones(state, actions) { const statesArray = []; for (let i = 0; i < actions.length; i++) { - if (actions[i].type === 'addProperties') { - const currentState = i ? { ...statesArray[i - 1] } : { ...state }; + switch (actions[i].type) { + case 'addProperties': { + const currentState = i ? { ...statesArray[i - 1] } : { ...state }; - Object.assign(currentState, actions[i].extraData); - statesArray.push(currentState); - } + Object.assign(currentState, actions[i].extraData); + statesArray.push(currentState); + break; + } - if (actions[i].type === 'removeProperties') { - const currentState = i ? { ...statesArray[i - 1] } : { ...state }; + case 'removeProperties': { + const currentState = i ? { ...statesArray[i - 1] } : { ...state }; - for (const key of actions[i].keysToRemove) { - delete currentState[key]; + for (const key of actions[i].keysToRemove) { + delete currentState[key]; + } + statesArray.push(currentState); + break; } - statesArray.push(currentState); - } - if (actions[i].type === 'clear') { - const currentState = i ? { ...statesArray[i - 1] } : { ...state }; + case 'clear': { + const currentState = i ? { ...statesArray[i - 1] } : { ...state }; - for (const key of Object.keys(currentState)) { - delete currentState[key]; + for (const key of Object.keys(currentState)) { + delete currentState[key]; + } + statesArray.push(currentState); + break; } - statesArray.push(currentState); + + default: throw new Error('Invalid action type'); } } From 6b77ee3abfdf9989420bd1a44afc52b62ab5b69b Mon Sep 17 00:00:00 2001 From: Kseniia Chepur Date: Tue, 3 Oct 2023 22:37:21 +0300 Subject: [PATCH 3/4] fix code as per the comments on github --- src/transformStateWithClones.js | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/transformStateWithClones.js b/src/transformStateWithClones.js index ee79249b0..b8aa9083f 100644 --- a/src/transformStateWithClones.js +++ b/src/transformStateWithClones.js @@ -8,39 +8,32 @@ */ function transformStateWithClones(state, actions) { const statesArray = []; + const stateCopy = { ...state }; - for (let i = 0; i < actions.length; i++) { - switch (actions[i].type) { + for (const action of actions) { + switch (action.type) { case 'addProperties': { - const currentState = i ? { ...statesArray[i - 1] } : { ...state }; - - Object.assign(currentState, actions[i].extraData); - statesArray.push(currentState); + Object.assign(stateCopy, action.extraData); break; } case 'removeProperties': { - const currentState = i ? { ...statesArray[i - 1] } : { ...state }; - - for (const key of actions[i].keysToRemove) { - delete currentState[key]; + for (const key of action.keysToRemove) { + delete stateCopy[key]; } - statesArray.push(currentState); break; } case 'clear': { - const currentState = i ? { ...statesArray[i - 1] } : { ...state }; - - for (const key of Object.keys(currentState)) { - delete currentState[key]; + for (const key of Object.keys(stateCopy)) { + delete stateCopy[key]; } - statesArray.push(currentState); break; } default: throw new Error('Invalid action type'); } + statesArray.push({ ...stateCopy }); } return statesArray; From 2c4ceb105634bf9a28990dfb8bb2ab5756c6e3f0 Mon Sep 17 00:00:00 2001 From: Kseniia Chepur Date: Wed, 4 Oct 2023 12:09:42 +0300 Subject: [PATCH 4/4] add destruction and variables for action types --- src/transformStateWithClones.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/transformStateWithClones.js b/src/transformStateWithClones.js index b8aa9083f..69584a4d9 100644 --- a/src/transformStateWithClones.js +++ b/src/transformStateWithClones.js @@ -7,31 +7,37 @@ * @return {Object[]} */ function transformStateWithClones(state, actions) { + const ADD_PROPS = 'addProperties'; + const REMOVE_PROPS = 'removeProperties'; + const CLEAR_ALL_PROPS = 'clear'; + const ERROR_MESSAGE = 'Invalid action type'; const statesArray = []; const stateCopy = { ...state }; for (const action of actions) { - switch (action.type) { - case 'addProperties': { - Object.assign(stateCopy, action.extraData); + const { type, extraData, keysToRemove } = action; + + switch (type) { + case ADD_PROPS: { + Object.assign(stateCopy, extraData); break; } - case 'removeProperties': { - for (const key of action.keysToRemove) { + case REMOVE_PROPS: { + for (const key of keysToRemove) { delete stateCopy[key]; } break; } - case 'clear': { + case CLEAR_ALL_PROPS: { for (const key of Object.keys(stateCopy)) { delete stateCopy[key]; } break; } - default: throw new Error('Invalid action type'); + default: throw new Error(ERROR_MESSAGE); } statesArray.push({ ...stateCopy }); }