From 8d5536056d2c6703c9dec0c6ba908c97a164902a Mon Sep 17 00:00:00 2001 From: maksym-mishchanchuk Date: Sun, 30 Jul 2023 23:42:52 +0300 Subject: [PATCH 1/2] initial commit --- package.json | 2 +- src/transformState.js | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index e83fa6dc7..55e4ae2de 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "license": "GPL-3.0", "devDependencies": { "@mate-academy/eslint-config": "*", - "@mate-academy/scripts": "^0.3.5", + "@mate-academy/scripts": "^1.2.8", "eslint": "^5.16.0", "eslint-plugin-jest": "^22.4.1", "eslint-plugin-node": "^8.0.1", diff --git a/src/transformState.js b/src/transformState.js index a1eaa0640..70ebba757 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++) { + const actionData = actions[i]; + + if (actionData.type === 'addProperties') { + for (const key in actionData.extraData) { + state[key] = actionData.extraData[key]; + } + } + + if (actionData.type === 'removeProperties') { + for (const key of actionData.keysToRemove) { + delete state[key]; + } + } + + if (actionData.type === 'clear') { + for (const key in state) { + delete state[key]; + } + } + } + + return state; } module.exports = transformState; From be75af424303fe3d4edd12609876ccb8289b64d8 Mon Sep 17 00:00:00 2001 From: maksym-mishchanchuk Date: Mon, 31 Jul 2023 16:37:53 +0300 Subject: [PATCH 2/2] restart task --- src/transformState.js | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/transformState.js b/src/transformState.js index 70ebba757..35b47bd68 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -5,29 +5,40 @@ * @param {Object[]} actions */ function transformState(state, actions) { - for (let i = 0; i < actions.length; i++) { - const actionData = actions[i]; + for (const key of actions) { - if (actionData.type === 'addProperties') { - for (const key in actionData.extraData) { - state[key] = actionData.extraData[key]; - } + if (key.type === 'addProperties') { + add(state, key.extraData); } - if (actionData.type === 'removeProperties') { - for (const key of actionData.keysToRemove) { - delete state[key]; - } + if (key.type === 'clear') { + clear(state); } - if (actionData.type === 'clear') { - for (const key in state) { - delete state[key]; - } + if (key.type === 'removeProperties') { + remove(state, key.keysToRemove); } } +} + +const add = (state, obj) => { + for (const key in obj) { + state[key] = obj[key]; + } return state; -} +}; + +const clear = (state) => { + for (const key in state) { + delete state[key]; + } +}; + +const remove = (state, keys) => { + for (const key of keys) { + delete state[key]; + } +}; module.exports = transformState;