Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/transformStateWithClones.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,33 @@
* @return {Object[]}
*/
function transformStateWithClones(state, actions) {
// write code here
const history = [];
let currentState = state;

for (const action of actions) {
let nextState = { ...currentState };

switch (action.type) {
case 'addProperties':
Object.assign(nextState, action.extraData);
break;

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

case 'clear':
nextState = {};
break;
}
Comment on lines 16 to 35

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation is very close! However, the switch statement is missing a default case for handling unknown action types. This violates checklist item #3: 'switch/case should always have default case for error handling'.


history.push(nextState);
currentState = nextState;
}

return history;
}

module.exports = transformStateWithClones;