diff --git a/src/transformStateWithClones.js b/src/transformStateWithClones.js index 96d4f7e7e..e9cab3746 100644 --- a/src/transformStateWithClones.js +++ b/src/transformStateWithClones.js @@ -1,13 +1,48 @@ -'use strict'; - -/** - * @param {Object} state - * @param {Object[]} actions - * - * @return {Object[]} - */ -function transformStateWithClones(state, actions) { - // write code here +// Додайте 'export' на початку +export function transformStateWithClones(state, actions) { + const history = []; + let currentState = { ...state }; + + for (const action of actions) { + // ... ваш код далі ... + } + + return history; } -module.exports = transformStateWithClones; + for (const action of actions) { + switch (action.type) { + case 'clear': + // Reset to an empty object + currentState = {}; + break; + + case 'addProperties': + // Merge current state with extraData using spread syntax + currentState = { + ...currentState, + ...action.extraData + }; + break; + + case 'removeProperties': + // Create a new copy to modify + const nextState = { ...currentState }; + action.keysToRemove.forEach(key => { + delete nextState[key]; + }); + currentState = nextState; + break; + + default: + // If an unknown action type is passed, we keep the state as is + break; + } + + // Push a new clone of the current state into our history array + history.push({ ...currentState }); + } + + return history; + +