From 273494bd932423e45701b72ce4bdb97ab8b4d73d Mon Sep 17 00:00:00 2001 From: Oleh Date: Tue, 15 Aug 2023 00:05:36 +0300 Subject: [PATCH] add solution --- src/transformState.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/transformState.js b/src/transformState.js index a1eaa0640..7f6bde831 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -5,7 +5,34 @@ * @param {Object[]} actions */ function transformState(state, actions) { - // write code here + for (const action of actions) { + switch (action.type) { + case 'addProperties': + if (action.extraData) { + Object.assign(state, action.extraData); + } + break; + + case 'removeProperties': + if (action.keysToRemove) { + for (const key of action.keysToRemove) { + delete state[key]; + } + } + break; + + case 'clear': + for (const key in state) { + if (state.hasOwnProperty(key)) { + delete state[key]; + } + } + break; + + default: + throw new Error(`Unknown action type: ${action.type}`); + } + } } module.exports = transformState;