Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgob committed Apr 16, 2016
0 parents commit 716dac1
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .babelrc
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"
]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
51 changes: 51 additions & 0 deletions lib/index.js
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
};
};
};
30 changes: 30 additions & 0 deletions package.json
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"
}
}
46 changes: 46 additions & 0 deletions src/index.js
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
})
}

0 comments on commit 716dac1

Please sign in to comment.