From 15f3cf73ac0404f99ab386a64c44601d80777309 Mon Sep 17 00:00:00 2001 From: Roman Perepelkin Date: Fri, 4 Aug 2023 13:07:15 +0300 Subject: [PATCH 1/2] Solution --- src/transformState.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/transformState.js b/src/transformState.js index a1eaa0640..f32c24f93 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -5,7 +5,25 @@ * @param {Object[]} actions */ function transformState(state, actions) { - // write code here + for (const obj of actions) { + if (obj.type === 'addProperties') { + Object.assign(state, obj.extraData); + } + + if (obj.type === 'removeProperties') { + for (const key of obj.keysToRemove) { + delete state[key]; + } + } + + if (obj.type === 'clear') { + for (const prop in state) { + delete state[prop]; + } + } + } + + return state; } module.exports = transformState; From f00b2a8ce49f99fc3afb1e5ef41354b3d1d2f7a0 Mon Sep 17 00:00:00 2001 From: Roman Perepelkin Date: Fri, 4 Aug 2023 15:14:02 +0300 Subject: [PATCH 2/2] Solution --- src/transformState.js | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/transformState.js b/src/transformState.js index f32c24f93..ce7a6c2c2 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -5,21 +5,32 @@ * @param {Object[]} actions */ function transformState(state, actions) { + const TYPE_ONE = 'addProperties'; + const TYPE_TWO = 'removeProperties'; + const TYPE_THREE = 'clear'; + for (const obj of actions) { - if (obj.type === 'addProperties') { - Object.assign(state, obj.extraData); - } + const { type } = obj; - if (obj.type === 'removeProperties') { - for (const key of obj.keysToRemove) { - delete state[key]; - } - } + switch (type) { + case TYPE_ONE: + Object.assign(state, obj.extraData); + break; + + case TYPE_TWO: + for (const key of obj.keysToRemove) { + delete state[key]; + } + break; + + case TYPE_THREE: + for (const prop in state) { + delete state[prop]; + } + break; - if (obj.type === 'clear') { - for (const prop in state) { - delete state[prop]; - } + default: + return state; } }