Skip to content

Commit

Permalink
add task solution
Browse files Browse the repository at this point in the history
  • Loading branch information
irahos committed Aug 1, 2023
1 parent 8de408a commit b460f2e
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/transformState.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,30 @@
* @param {Object[]} actions
*/
function transformState(state, actions) {
// write code here
for (const action of actions) {
switch (action.type) {
case 'addProperties':
Object.assign(state, action.extraData);
break;

case 'removeProperties':
action.keysToRemove.forEach((key) => {
if (state.hasOwnProperty(key)) {
delete state[key];
}
});
break;

case 'clear':
Object.keys(state).forEach((key) => delete state[key]);
break;

default:
throw Error('Invalid input.');
}
}

return state;
}

module.exports = transformState;

0 comments on commit b460f2e

Please sign in to comment.