From de010d7b30b1ae59b6cd52815d326c5c5348b578 Mon Sep 17 00:00:00 2001 From: Oleksandr Podvoiskyi Date: Wed, 4 Oct 2023 16:16:51 +0300 Subject: [PATCH] add task solution --- src/transformStateWithClones.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/transformStateWithClones.js b/src/transformStateWithClones.js index 96d4f7e7e..5c3446384 100644 --- a/src/transformStateWithClones.js +++ b/src/transformStateWithClones.js @@ -7,7 +7,33 @@ * @return {Object[]} */ function transformStateWithClones(state, actions) { - // write code here + const result = []; + const cloneState = { ...state }; + + for (const action of actions) { + switch (action.type) { + case 'addProperties': + Object.assign(cloneState, action.extraData); + break; + + case 'removeProperties': + action.keysToRemove.forEach(item => delete cloneState[item]); + break; + + case 'clear': + for (const key in cloneState) { + delete cloneState[key]; + } + break; + + default: + break; + } + + result.push({ ...cloneState }); + } + + return result; } module.exports = transformStateWithClones;