Skip to content

Commit

Permalink
statefulClones
Browse files Browse the repository at this point in the history
  • Loading branch information
IOherhi committed Dec 27, 2024
1 parent f7f4228 commit 982582e
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions src/transformStateWithClones.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,55 @@
*/
function transformStateWithClones(state, actions) {
const result = [];
const stateCopy = { ...state };

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

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

case 'clear':
result.push(clear(state));
result.push(clear(stateCopy));
break;
}
}

return result;
}

function addProperties(state, extraData) {
const stateA = {};
function addProperties(stateCopy, extraData) {
Object.assign(stateCopy, extraData);

Object.assign(stateA, state, extraData);
let stateA = {}; /* №1 */

stateA = { ...stateCopy };

return stateA;
}

function removeProperties(state, keysToRemove) {
/* const stateB = {};
function removeProperties(stateCopy, keysToRemove) {
for (const key of keysToRemove) {
delete state[key];
delete stateCopy[key];
}

Object.assign(stateB, state);
return stateB; */

const stateB = { ...state };

for (const key of keysToRemove) {
delete stateB[key];
}
const stateB = { ...stateCopy };

return stateB;
}

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

const stateCopyB = {};

return emptyObject;
return stateCopyB;
}

module.exports = transformStateWithClones;

0 comments on commit 982582e

Please sign in to comment.