Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Yevheniia-Sid committed Aug 2, 2023
1 parent 35521f9 commit 945c869
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/transformState.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,42 @@
* @param {Object} state
* @param {Object[]} actions
*/
function addProperties(state, addObject) {
for (const addProperty in addObject) {
state[addProperty] = addObject[addProperty];
}
}

function removeProperties(state, arrProperties) {
for (const removeProperty of arrProperties) {
delete state[removeProperty];
}
}

function removeAllProperty(state) {
for (const key in state) {
delete state[key];
}
}

function transformState(state, actions) {
for (const action of actions) {
if (action.type === 'addProperties') {
for (const addProperty in action.extraData) {
state[addProperty] = action.extraData[addProperty];
for (const item of actions) {
switch (item.type) {
case 'addProperties': {
addProperties(state, item.extraData);
break;
}
} else if (action.type === 'removeProperties') {
for (const removeProperty of action.keysToRemove) {
delete state[removeProperty];

case 'removeProperties': {
removeProperties(state, item.keysToRemove);
break;
}
} else if (action.type === 'clear') {
for (const key in state) {
delete state[key];

case 'clear': {
removeAllProperty(state);
break;
}
default: throw new Error('object does not know this type');
}
}

Expand Down

0 comments on commit 945c869

Please sign in to comment.