-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d76f5a8
commit 3aa537e
Showing
1 changed file
with
19 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,30 @@ | ||
"use strict"; | ||
|
||
function transformState(state, actions) { | ||
const newState = { ...state }; | ||
|
||
for (const action of actions) { | ||
if (action.type === "addProperties") { | ||
if (typeof action.extraData === "object") { | ||
for (const key in action.extraData) { | ||
newState[key] = action.extraData[key]; | ||
const { type, extraData, keysToRemove } = action; | ||
|
||
switch (type) { | ||
case "addProperties": | ||
Object.assign(state, extraData); | ||
break; | ||
|
||
case "removeProperties": | ||
for (const key of keysToRemove) { | ||
delete state[key]; | ||
} | ||
} | ||
} else if (action.type === "removeProperties") { | ||
if (Array.isArray(action.keysToRemove)) { | ||
for (const key of action.keysToRemove) { | ||
if (newState.hasOwnProperty(key)) { | ||
delete newState[key]; | ||
} | ||
break; | ||
|
||
case "clear": | ||
for (const key in state) { | ||
delete state[key]; | ||
} | ||
} | ||
} else if (action.type === "clear") { | ||
for (const key in newState) { | ||
delete newState[key]; | ||
} | ||
break; | ||
|
||
default: | ||
return "Error: Type Unknown"; | ||
} | ||
} | ||
|
||
return newState; | ||
} | ||
|
||
module.exports = transformState; |