-
Notifications
You must be signed in to change notification settings - Fork 1
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
0 parents
commit 716dac1
Showing
5 changed files
with
136 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
plugins: [ | ||
"transform-es2015-arrow-functions", | ||
["transform-es2015-spread", { "loose": true }], | ||
"transform-es2015-parameters", | ||
"transform-object-rest-spread" | ||
] | ||
} |
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 @@ | ||
node_modules/ |
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,51 @@ | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
|
||
const getReducerName = function (reducerName) { | ||
return reducerName + '_SET_STATE'; | ||
}; | ||
|
||
function createReducer(initialState, handlers) { | ||
return function reducer() { | ||
let state = arguments.length <= 0 || arguments[0] === undefined ? initialState : arguments[0]; | ||
let action = arguments[1]; | ||
|
||
if (handlers.hasOwnProperty(action.type)) { | ||
return handlers[action.type](state, action); | ||
} else { | ||
return state; | ||
} | ||
}; | ||
} | ||
|
||
export const createBasicReducer = function (reducerName, initialState) { | ||
|
||
const _initialState = _extends({}, initialState, { | ||
reducerName | ||
}); | ||
|
||
return createReducer(_initialState, { | ||
[getReducerName(reducerName)]: function (state, action) { | ||
return _extends({}, state, action.state); | ||
} | ||
|
||
}); | ||
}; | ||
|
||
export const getReducerState = function (store, reducerName) { | ||
return function () { | ||
const globalState = store.getState(); | ||
const stateReducerName = Object.keys(globalState).find(function (p) { | ||
return globalState[p].reducerName === reducerName; | ||
}); | ||
return globalState[stateReducerName]; | ||
}; | ||
}; | ||
|
||
export const createUpdateStateAction = function (reducerName) { | ||
return function (state) { | ||
return { | ||
type: getReducerName(reducerName), | ||
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,30 @@ | ||
{ | ||
"name": "reduxerit", | ||
"version": "0.1.0", | ||
"description": "redux utils to generate reducer and actions in a shorter way", | ||
"main": "index.js", | ||
"keywords": [ | ||
"react", | ||
"reactjs", | ||
"redux", | ||
"flux" | ||
], | ||
"scripts": { | ||
"build:lib": "babel src --out-dir lib", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": { | ||
"name": "Jurgo Boemo", | ||
"email": "[email protected]", | ||
"url": "http://jurgo.me/" | ||
}, | ||
"license": "ISC", | ||
"devDependencies": { | ||
"babel": "^6.5.2", | ||
"babel-cli": "^6.7.5", | ||
"babel-plugin-transform-es2015-arrow-functions": "^6.5.2", | ||
"babel-plugin-transform-es2015-parameters": "^6.7.0", | ||
"babel-plugin-transform-es2015-spread": "^6.6.5", | ||
"babel-plugin-transform-object-rest-spread": "^6.6.5" | ||
} | ||
} |
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,46 @@ | ||
|
||
const getReducerName = (reducerName) => reducerName+ '_SET_STATE'; | ||
|
||
function createReducer(initialState, handlers) { | ||
return function reducer(state = initialState, action) { | ||
if (handlers.hasOwnProperty(action.type)) { | ||
return handlers[action.type](state, action) | ||
} | ||
else { | ||
return state | ||
} | ||
} | ||
} | ||
|
||
export const createBasicReducer = (reducerName , initialState) => { | ||
|
||
const _initialState = { | ||
...initialState, | ||
reducerName | ||
} | ||
|
||
return createReducer( | ||
_initialState, | ||
{ | ||
[getReducerName(reducerName)] : (state, action) => ({ | ||
...state, | ||
...action.state | ||
} | ||
) | ||
|
||
})} | ||
|
||
export const getReducerState = (store, reducerName) => { | ||
return function(){ | ||
const globalState = store.getState() | ||
const stateReducerName = Object.keys(globalState).find((p) => globalState[p].reducerName === reducerName ) | ||
return globalState[stateReducerName] | ||
} | ||
} | ||
|
||
export const createUpdateStateAction = (reducerName) => { | ||
return (state) => ({ | ||
type:getReducerName(reducerName), | ||
state | ||
}) | ||
} |