Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
Egor Mamtsev authored and Egor Mamtsev committed Dec 10, 2024
1 parent 7aa3e8a commit e508c10
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/transformStateWithClones.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,36 @@
* @return {Object[]}
*/
function transformStateWithClones(state, actions) {
// write code here
const history = [];

function clone(source) {
return { ...source };
}

let objCopy = clone(state);

for (let i = 0; i < actions.length; i++) {
switch (actions[i].type) {
case 'addProperties':
for (const key in actions[i].extraData) {
objCopy[key] = actions[i].extraData[key];
}
history.push(clone(objCopy));
break;
case 'removeProperties':
for (let d = 0; d < actions[i].keysToRemove.length; d++) {
delete objCopy[actions[i].keysToRemove[d]];
}
history.push(clone(objCopy));
break;
case 'clear':
objCopy = {};
history.push(clone(objCopy));
break;
}
}

return history;
}

module.exports = transformStateWithClones;

0 comments on commit e508c10

Please sign in to comment.