diff --git a/src/transformStateWithClones.js b/src/transformStateWithClones.js index 96d4f7e7e..69584a4d9 100644 --- a/src/transformStateWithClones.js +++ b/src/transformStateWithClones.js @@ -7,7 +7,42 @@ * @return {Object[]} */ function transformStateWithClones(state, actions) { - // write code here + 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) { + const { type, extraData, keysToRemove } = action; + + switch (type) { + case ADD_PROPS: { + Object.assign(stateCopy, extraData); + break; + } + + case REMOVE_PROPS: { + for (const key of keysToRemove) { + delete stateCopy[key]; + } + break; + } + + case CLEAR_ALL_PROPS: { + for (const key of Object.keys(stateCopy)) { + delete stateCopy[key]; + } + break; + } + + default: throw new Error(ERROR_MESSAGE); + } + statesArray.push({ ...stateCopy }); + } + + return statesArray; } module.exports = transformStateWithClones;