Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #4122

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Develop #4122

Show file tree
Hide file tree
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
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "GPL-3.0",
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^1.9.12",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
53 changes: 52 additions & 1 deletion src/transformStateWithClones.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,60 @@
* @param {Object[]} actions
*
* @return {Object[]}
*
* transformStateWithClones()
*/
function transformStateWithClones(state, actions) {
// write code here
const result = [];
const stateCopy = { ...state };

for (const action of actions) {
switch (action.type) {
case 'addProperties':
result.push(addProperties(stateCopy, action.extraData));

Choose a reason for hiding this comment

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

Issue: The stateCopy object is modified directly within the switch cases. This can lead to unintended side effects as the same object is used for all actions.

Suggestion: Instead of pushing the result inside each case, modify the stateCopy within the case and push a new copy of the modified state to the result array after the switch block, at the end of each loop iteration.

break;

case 'removeProperties':
result.push(removeProperties(stateCopy, action.keysToRemove));

Choose a reason for hiding this comment

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

Issue: The stateCopy object is modified directly within the switch cases. This can lead to unintended side effects as the same object is used for all actions.

Suggestion: Instead of pushing the result inside each case, modify the stateCopy within the case and push a new copy of the modified state to the result array after the switch block, at the end of each loop iteration.

break;

case 'clear':
result.push(clear(stateCopy));

Choose a reason for hiding this comment

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

Issue: The stateCopy object is modified directly within the switch cases. This can lead to unintended side effects as the same object is used for all actions.

Suggestion: Instead of pushing the result inside each case, modify the stateCopy within the case and push a new copy of the modified state to the result array after the switch block, at the end of each loop iteration.

break;
}
}

return result;
}

function addProperties(stateCopy, extraData) {
Object.assign(stateCopy, extraData);

let stateA = {}; /* №1 */

stateA = { ...stateCopy };

return stateA;
}

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

const stateB = { ...stateCopy };

return stateB;
}

function clear(stateCopy) {
for (const cler in stateCopy) {
delete stateCopy[cler];
}

const stateCopyB = {};

return stateCopyB;
}

module.exports = transformStateWithClones;
Loading