From e78087532a0ae5ca257a87a0bd86c011ed3ec716 Mon Sep 17 00:00:00 2001 From: Kasia Kopec Date: Mon, 9 Dec 2024 17:42:48 +0100 Subject: [PATCH] solving task --- src/transformStateWithClones.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/transformStateWithClones.js b/src/transformStateWithClones.js index 96d4f7e7e..b8a95926b 100644 --- a/src/transformStateWithClones.js +++ b/src/transformStateWithClones.js @@ -7,7 +7,30 @@ * @return {Object[]} */ function transformStateWithClones(state, actions) { - // write code here -} + const stateHistory = []; + let currentState = { ...state }; + + actions.forEach((action) => { + switch (action.type) { + case 'clear': + currentState = {}; + break; + case 'addProperties': + currentState = { ...currentState, ...action.extraData }; + break; + case 'removeProperties': + action.keysToRemove.forEach((key) => { + delete currentState[key]; + }); + break; + default: + throw new Error(`Unknown action type: ${action.type}`); + } + stateHistory.push({ ...currentState }); + }); + + return stateHistory; + } + module.exports = transformStateWithClones;