Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-mate committed Dec 19, 2024
1 parent 7aa3e8a commit 54fb23d
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion src/transformStateWithClones.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,65 @@
*
* @return {Object[]}
*/

function transformStateWithClones(state, actions) {
// write code here
const hystoryArray = [];
const stateCopy = { ...state };

for (const action of actions) {
const newCopy = Object.assign({}, hystoryArray[hystoryArray.length - 1]);

switch (action.type) {
case 'addProperties':
if (hystoryArray.length >= 1) {
hystoryArray.push(addProperties(newCopy, action.extraData));
break;
} else {
hystoryArray.push(addProperties(stateCopy, action.extraData));
break;
}

case 'removeProperties':
if (hystoryArray.length >= 1) {
hystoryArray.push(removeProperties(newCopy, action.keysToRemove));
break;
} else {
hystoryArray.push(removeProperties(stateCopy, action.keysToRemove));
break;
}

case 'clear':
if (hystoryArray.length >= 1) {
hystoryArray.push(clearProperties(newCopy));
break;
} else {
hystoryArray.push(clearProperties(stateCopy));
break;
}
}
}

function addProperties(stateCopyObject, extraData) {
return Object.assign(stateCopyObject, extraData);
}

function removeProperties(stateCopyObject, keysToRemove) {
for (const key of keysToRemove) {
delete stateCopyObject[key];
}

return stateCopyObject;
}

function clearProperties(stateCopyObject) {
for (const key in stateCopyObject) {
delete stateCopyObject[key];
}

return stateCopyObject;
}

return hystoryArray;
}

module.exports = transformStateWithClones;

0 comments on commit 54fb23d

Please sign in to comment.