Skip to content
Open
Changes from all 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
30 changes: 29 additions & 1 deletion src/transformStateWithClones.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,35 @@
* @return {Object[]}
*/
function transformStateWithClones(state, actions) {
// write code here
const stateHistory = [];
let currentState = { ...state };

for (const action of actions) {
switch (action.type) {
case 'clear':
currentState = {};
break;

case 'addProperties':
currentState = { ...currentState, ...action.extraData };
break;

case 'removeProperties':
currentState = { ...currentState };

for (const key of action.keysToRemove) {
delete currentState[key];
}
break;
default:
// handle unknown action types explicitly for safety
throw new Error(`Unknown action type: ${action.type}`);
}

stateHistory.push({ ...currentState });
}

return stateHistory;
}

module.exports = transformStateWithClones;