-
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
Solution #4118
base: master
Are you sure you want to change the base?
Solution #4118
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,47 @@ | |
* @return {Object[]} | ||
*/ | ||
function transformStateWithClones(state, actions) { | ||
// write code here | ||
const stateHistory = []; | ||
|
||
let currentState = { ...state }; // Create an initial copy of the state | ||
|
||
for (const action of actions) { | ||
switch (action.type) { | ||
case 'addProperties': | ||
currentState = addProperties(currentState, action.extraData); | ||
break; | ||
|
||
case 'removeProperties': | ||
currentState = removeProperties(currentState, action.keysToRemove); | ||
break; | ||
|
||
case 'clear': | ||
currentState = clearProperties(currentState); | ||
break; | ||
} | ||
|
||
stateHistory.push(currentState); | ||
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. The current implementation pushes the 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. The current implementation pushes |
||
} | ||
|
||
return stateHistory; | ||
} | ||
|
||
function addProperties(state, extraData) { | ||
return { ...state, ...extraData }; // Create a new object with added propertie | ||
} | ||
|
||
function removeProperties(state, keysToRemove) { | ||
const newState = { ...state }; // Clone the state | ||
|
||
for (const key of keysToRemove) { | ||
delete newState[key]; // Remove the specified keys | ||
} | ||
|
||
return newState; | ||
} | ||
|
||
function clearProperties(state) { | ||
return {}; // Return a new empty object | ||
} | ||
|
||
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.
Consider adding a
default
case in theswitch
statement to handle unexpected action types. This will improve error handling and make the code more robust.