Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
joanna-konopacka committed Oct 30, 2023
1 parent d76f5a8 commit 3aa537e
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions src/transformState.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 3aa537e

Please sign in to comment.