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

Task solution #2827

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 36 additions & 1 deletion src/transformStateWithClones.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,42 @@
* @return {Object[]}
*/
function transformStateWithClones(state, actions) {
// write code here
const ADD_PROPS = 'addProperties';
const REMOVE_PROPS = 'removeProperties';
const CLEAR_ALL_PROPS = 'clear';
const ERROR_MESSAGE = 'Invalid action type';
const statesArray = [];

Choose a reason for hiding this comment

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

Suggested change
const statesArray = [];
const statesArray = [];
const stateCopy = { ...state };

const stateCopy = { ...state };

for (const action of actions) {
const { type, extraData, keysToRemove } = action;

switch (type) {
case ADD_PROPS: {
Object.assign(stateCopy, extraData);
break;
}

case REMOVE_PROPS: {
for (const key of keysToRemove) {
delete stateCopy[key];
}
break;
}

case CLEAR_ALL_PROPS: {
for (const key of Object.keys(stateCopy)) {
delete stateCopy[key];
}
break;
}

default: throw new Error(ERROR_MESSAGE);
}

Choose a reason for hiding this comment

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

Suggested change
}
}
statesArray.push({ ...stateCopy });

statesArray.push({ ...stateCopy });
}

return statesArray;
}

module.exports = transformStateWithClones;
Loading