-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
base: master
Are you sure you want to change the base?
Develop #4122
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
break; | ||
|
||
case 'removeProperties': | ||
result.push(removeProperties(stateCopy, action.keysToRemove)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: The Suggestion: Instead of pushing the result inside each case, modify the |
||
break; | ||
|
||
case 'clear': | ||
result.push(clear(stateCopy)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: The Suggestion: Instead of pushing the result inside each case, modify the |
||
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; |
There was a problem hiding this comment.
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 theswitch
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 theresult
array after theswitch
block, at the end of each loop iteration.