diff --git a/src/transformStateWithClones.js b/src/transformStateWithClones.js index 96d4f7e7e..13cb01c8d 100644 --- a/src/transformStateWithClones.js +++ b/src/transformStateWithClones.js @@ -7,7 +7,41 @@ * @return {Object[]} */ function transformStateWithClones(state, actions) { - // write code here + const history = []; + let currentState = { ...state }; + + for (const action of actions) { + switch (action.type) { + case 'clear': + currentState = {}; + break; + + case 'addProperties': + currentState = { + ...currentState, + ...action.extraData, + }; + break; + + case 'removeProperties': { + const nextState = { ...currentState }; + + for (const key of action.keysToRemove) { + delete nextState[key]; + } + + currentState = nextState; + break; + } + + default: + throw new Error(`Unknown action type: ${action.type}`); + } + + history.push(currentState); + } + + return history; } module.exports = transformStateWithClones;