diff --git a/src/transformStateWithClones.js b/src/transformStateWithClones.js index 96d4f7e7e..36f3d30bd 100644 --- a/src/transformStateWithClones.js +++ b/src/transformStateWithClones.js @@ -7,7 +7,40 @@ * @return {Object[]} */ function transformStateWithClones(state, actions) { - // write code here + let currentState = { ...state }; + const history = []; + + for (const action of actions) { + let nextState = { ...currentState }; + + switch (action.type) { + case 'addProperties': + if (action.extraData && typeof action.extraData === 'object') { + nextState = { ...currentState, ...action.extraData }; + } + break; + + case 'removeProperties': + if (Array.isArray(action.keysToRemove)) { + for (const key of action.keysToRemove) { + delete nextState[key]; + } + } + break; + + case 'clear': + nextState = {}; + break; + + default: + continue; + } + + history.push(nextState); + currentState = nextState; + } + + return history; } module.exports = transformStateWithClones;