diff --git a/src/transformStateWithClones.js b/src/transformStateWithClones.js index 96d4f7e7e..072b9d07d 100644 --- a/src/transformStateWithClones.js +++ b/src/transformStateWithClones.js @@ -7,7 +7,48 @@ * @return {Object[]} */ function transformStateWithClones(state, actions) { - // write code here + const result = []; + let currentState = { ...state }; + + for (const action of actions) { + switch (action.type) { + case 'addProperties': + currentState = addProperties(currentState, action.extraData); + + break; + + case 'removeProperties': + currentState = removeProperties(currentState, action.keysToRemove); + + break; + + case 'clear': + currentState = clear(currentState); + + break; + } + result.push(currentState); + } + + return result; +} + +function addProperties(state, extraData) { + return { ...state, ...extraData }; +} + +function removeProperties(state, keysToRemove) { + const newState = { ...state }; + + for (const key of keysToRemove) { + delete newState[key]; + } + + return newState; +} + +function clear(state) { + return {}; } module.exports = transformStateWithClones;