From bc39a32561b397141b523081d7403d94dbe64e7f Mon Sep 17 00:00:00 2001 From: MateuszSlezal Date: Wed, 26 Jul 2023 13:40:52 +0200 Subject: [PATCH 1/2] add task solution --- src/transformState.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/transformState.js b/src/transformState.js index a1eaa0640..1c0835c64 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -5,7 +5,36 @@ * @param {Object[]} actions */ function transformState(state, actions) { - // write code here + const stateHistory = []; + + for (const order of actions) { + const ADD_PROPERTIES = order.type === 'addProperties'; + const REMOVE_PROPERTIES = order.type === 'removeProperties'; + const CLEAR_ALL = order.type === 'clear'; + + if (ADD_PROPERTIES) { + Object.assign(state, order.extraData); + stateHistory.push(Object.assign({}, state)); + } + + if (REMOVE_PROPERTIES) { + for (const remove of order.keysToRemove) { + delete state[remove]; + } + + stateHistory.push(Object.assign({}, state)); + } + + if (CLEAR_ALL) { + for (const key in state) { + delete state[key]; + } + + stateHistory.push(Object.assign({}, state)); + } + } + + return state; } module.exports = transformState; From 10b310c9d4d9190255eaa0ccc1119e1a08df7ed0 Mon Sep 17 00:00:00 2001 From: MateuszSlezal Date: Mon, 31 Jul 2023 00:45:27 +0200 Subject: [PATCH 2/2] changes --- src/transformState.js | 48 ++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/transformState.js b/src/transformState.js index 1c0835c64..3c743d69b 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -4,33 +4,35 @@ * @param {Object} state * @param {Object[]} actions */ +const ADD_PROPERTIES = 'addProperties'; +const REMOVE_PROPERTIES = 'removeProperties'; +const CLEAR_ALL = 'clear'; + function transformState(state, actions) { const stateHistory = []; for (const order of actions) { - const ADD_PROPERTIES = order.type === 'addProperties'; - const REMOVE_PROPERTIES = order.type === 'removeProperties'; - const CLEAR_ALL = order.type === 'clear'; - - if (ADD_PROPERTIES) { - Object.assign(state, order.extraData); - stateHistory.push(Object.assign({}, state)); - } - - if (REMOVE_PROPERTIES) { - for (const remove of order.keysToRemove) { - delete state[remove]; - } - - stateHistory.push(Object.assign({}, state)); - } - - if (CLEAR_ALL) { - for (const key in state) { - delete state[key]; - } - - stateHistory.push(Object.assign({}, state)); + switch (order.type) { + case ADD_PROPERTIES: + Object.assign(state, order.extraData); + stateHistory.push(Object.assign({}, state)); + break; + + case REMOVE_PROPERTIES: + for (const remove of order.keysToRemove) { + delete state[remove]; + } + + stateHistory.push(Object.assign({}, state)); + break; + + case CLEAR_ALL: + for (const key in state) { + delete state[key]; + } + + stateHistory.push(Object.assign({}, state)); + break; } }