From 9be9f3e13b4707d9f5aee7ebc1127e428c621be6 Mon Sep 17 00:00:00 2001 From: Olena Nimakova Date: Sat, 2 Sep 2023 22:31:42 +0300 Subject: [PATCH] transformState --- src/transformState.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/transformState.js b/src/transformState.js index a1eaa0640..e8ee63a2d 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -5,7 +5,29 @@ * @param {Object[]} actions */ function transformState(state, actions) { - // write code here + for (let i = 0; i < actions.length; i++) { + if (actions[i].type === 'addProperties') { + Object.assign(state, actions[i].extraData); + } + + if (actions[i].type === 'removeProperties') { + for (const key of actions[i].keysToRemove) { + if (state.hasOwnProperty(key)) { + delete state[key]; + } + } + } + + if (actions[i].type === 'clear') { + for (const key in state) { + if (state.hasOwnProperty(key)) { + delete state[key]; + } + } + } + } + + return state; } module.exports = transformState;