Skip to content

Commit

Permalink
stateful object solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Livan94 committed Jul 14, 2023
1 parent 8de408a commit 059f2f1
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/transformState.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,36 @@
* @param {Object[]} actions
*/
function transformState(state, actions) {
// write code here
for (const action of actions) {
switch (action.type) {
case 'addProperties':
if (action.extraData) {
for (const [key, value] of Object.entries(action.extraData)) {
state[key] = value;
}
}
break;

case 'removeProperties':
if (action.keysToRemove) {
for (const key of action.keysToRemove) {
if (state.hasOwnProperty(key)) {
delete state[key];
}
}
}
break;

case 'clear':
for (const key in state) {
delete state[key];
}
break;

default:
break;
}
}
}

module.exports = transformState;

0 comments on commit 059f2f1

Please sign in to comment.