From c3404b77b1a9bf6ecc724b3bad62f320a0380d47 Mon Sep 17 00:00:00 2001 From: Joanna Konopacka Date: Mon, 30 Oct 2023 20:52:27 +0100 Subject: [PATCH 1/7] solution --- src/transformState.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/transformState.js b/src/transformState.js index a1eaa064..07eb7d58 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -1,11 +1,24 @@ -'use strict'; - -/** - * @param {Object} state - * @param {Object[]} actions - */ +"use strict"; function transformState(state, actions) { - // write code here + for (const action of actions) { + if (action.type === "addProperties") { + if (typeof action.extraData === "object") { + state = { ...state, ...action.extraData }; + } + } else if (action.type === "removeProperties") { + if (Array.isArray(action.keysToRemove)) { + state = Object.keys(state) + .filter((key) => !action.keysToRemove.includes(key)) + .reduce((newState, key) => { + newState[key] = state[key]; + return newState; + }, {}); + } + } else if (action.type === "clear") { + state = {}; + } + } + return state; } module.exports = transformState; From 91345916f1deddba8f1e64f18611c60ca0c91557 Mon Sep 17 00:00:00 2001 From: Joanna Konopacka Date: Mon, 30 Oct 2023 20:55:58 +0100 Subject: [PATCH 2/7] solution --- src/transformState.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/transformState.js b/src/transformState.js index 07eb7d58..f854fbeb 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -1,24 +1,26 @@ "use strict"; function transformState(state, actions) { + let newState = { ...state }; for (const action of actions) { if (action.type === "addProperties") { if (typeof action.extraData === "object") { - state = { ...state, ...action.extraData }; + newState = { ...newState, ...action.extraData }; } } else if (action.type === "removeProperties") { if (Array.isArray(action.keysToRemove)) { - state = Object.keys(state) + newState = Object.keys(newState) .filter((key) => !action.keysToRemove.includes(key)) - .reduce((newState, key) => { - newState[key] = state[key]; - return newState; + .reduce((filteredState, key) => { + filteredState[key] = newState[key]; + return filteredState; }, {}); } } else if (action.type === "clear") { - state = {}; + newState = {}; } } - return state; + + return newState; } module.exports = transformState; From 8ad514f8e61748d89c695f7aaebd1beac2a2c248 Mon Sep 17 00:00:00 2001 From: Joanna Konopacka Date: Mon, 30 Oct 2023 21:57:30 +0100 Subject: [PATCH 3/7] solution --- src/transformState.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/transformState.js b/src/transformState.js index f854fbeb..8faa8607 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -1,4 +1,5 @@ "use strict"; + function transformState(state, actions) { let newState = { ...state }; for (const action of actions) { From d76f5a8bc7d4e899145e7d56da2bb82395951b47 Mon Sep 17 00:00:00 2001 From: Joanna Konopacka Date: Mon, 30 Oct 2023 22:01:24 +0100 Subject: [PATCH 4/7] solution --- src/transformState.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/transformState.js b/src/transformState.js index 8faa8607..023496a5 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -1,23 +1,27 @@ "use strict"; function transformState(state, actions) { - let newState = { ...state }; + const newState = { ...state }; + for (const action of actions) { if (action.type === "addProperties") { if (typeof action.extraData === "object") { - newState = { ...newState, ...action.extraData }; + for (const key in action.extraData) { + newState[key] = action.extraData[key]; + } } } else if (action.type === "removeProperties") { if (Array.isArray(action.keysToRemove)) { - newState = Object.keys(newState) - .filter((key) => !action.keysToRemove.includes(key)) - .reduce((filteredState, key) => { - filteredState[key] = newState[key]; - return filteredState; - }, {}); + for (const key of action.keysToRemove) { + if (newState.hasOwnProperty(key)) { + delete newState[key]; + } + } } } else if (action.type === "clear") { - newState = {}; + for (const key in newState) { + delete newState[key]; + } } } From 3aa537ea0b0e83f3ab020fae480cfcfd69cc7d92 Mon Sep 17 00:00:00 2001 From: Joanna Konopacka Date: Mon, 30 Oct 2023 22:04:07 +0100 Subject: [PATCH 5/7] solution --- src/transformState.js | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/transformState.js b/src/transformState.js index 023496a5..44bb8ab6 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -1,31 +1,30 @@ "use strict"; function transformState(state, actions) { - const newState = { ...state }; - for (const action of actions) { - if (action.type === "addProperties") { - if (typeof action.extraData === "object") { - for (const key in action.extraData) { - newState[key] = action.extraData[key]; + const { type, extraData, keysToRemove } = action; + + switch (type) { + case "addProperties": + Object.assign(state, extraData); + break; + + case "removeProperties": + for (const key of keysToRemove) { + delete state[key]; } - } - } else if (action.type === "removeProperties") { - if (Array.isArray(action.keysToRemove)) { - for (const key of action.keysToRemove) { - if (newState.hasOwnProperty(key)) { - delete newState[key]; - } + break; + + case "clear": + for (const key in state) { + delete state[key]; } - } - } else if (action.type === "clear") { - for (const key in newState) { - delete newState[key]; - } + break; + + default: + return "Error: Type Unknown"; } } - - return newState; } module.exports = transformState; From 5249124dba7df72b68897c3158ca28d2fb49e33f Mon Sep 17 00:00:00 2001 From: Joanna Konopacka Date: Wed, 1 Nov 2023 18:15:09 +0100 Subject: [PATCH 6/7] solution --- src/transformState.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/transformState.js b/src/transformState.js index 44bb8ab6..fc20fa5e 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -2,27 +2,33 @@ function transformState(state, actions) { for (const action of actions) { - const { type, extraData, keysToRemove } = action; + const { type } = action; switch (type) { case "addProperties": - Object.assign(state, extraData); + const { extraData } = action; + if (extraData) { + Object.assign(state, extraData); + } break; case "removeProperties": - for (const key of keysToRemove) { - delete state[key]; + const { keysToRemove } = action; + if (keysToRemove) { + for (const key of keysToRemove) { + delete state[key]; + } } break; case "clear": - for (const key in state) { + Object.keys(state).forEach((key) => { delete state[key]; - } + }); break; default: - return "Error: Type Unknown"; + console.error("Error: Type Unknown"); } } } From 6d026f94796445eb0aaa1adcb48cc2575338d6ea Mon Sep 17 00:00:00 2001 From: Joanna Konopacka Date: Wed, 1 Nov 2023 18:17:50 +0100 Subject: [PATCH 7/7] solution --- src/transformState.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformState.js b/src/transformState.js index fc20fa5e..2f9ae512 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -28,7 +28,7 @@ function transformState(state, actions) { break; default: - console.error("Error: Type Unknown"); + break; } } }