-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added src/state Allows management of state. immutable and mutable work, but not with get and set. Extends properties to the current state object. Updated code otherwise to include references.
- Loading branch information
Dustyn Blackmore
committed
May 16, 2018
1 parent
84de2dd
commit 9c9c877
Showing
6 changed files
with
101 additions
and
70 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
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
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
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = (state) => ({ | ||
get: (property) => { | ||
return state.property || undefined; | ||
} | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const immutable = require('./immutable'); | ||
const mutable = require('./mutable'); | ||
|
||
module.exports = (type) => (initialState) => { | ||
let state = {}; | ||
|
||
if (typeof initialState !== 'undefined') { | ||
state = initialState; | ||
} | ||
|
||
if (typeof type === 'string' && (type === 'mutable' || type === 'immutable')) { | ||
if (type === 'immutable') { | ||
return Object.freeze(Object.assign( | ||
state, | ||
immutable(state), | ||
)); | ||
} | ||
|
||
if (type === 'mutable') { | ||
return Object.assign( | ||
state, | ||
mutable(state), | ||
) | ||
} | ||
} | ||
|
||
return state; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const checkSetObject = (setObject) => { | ||
return typeof Object.keys(setObject)[0] !== undefined; | ||
} | ||
|
||
module.exports = (state) => ({ | ||
get: (property) => { | ||
return state[property] || undefined; | ||
}, | ||
set: (setObject) => { | ||
if (checkSetObject(setObject)) { | ||
let currentState = Object.assign({}, state); | ||
|
||
let targetProperty = Object.keys(setObject)[0]; | ||
let targetValue = setObject[targetProperty]; | ||
|
||
console.log('Attempting to set %s, with %s.', targetProperty, targetValue); | ||
let newState = Object.assign({ [targetProperty]: targetValue }, currentState); | ||
|
||
state = newState; | ||
} | ||
} | ||
}); |