Skip to content

Instantly share code, notes, and snippets.

@amoilanen
Created May 13, 2016 08:26
Show Gist options
  • Save amoilanen/67274bc7b02bdde72e305d1e86a2d86a to your computer and use it in GitHub Desktop.
Save amoilanen/67274bc7b02bdde72e305d1e86a2d86a to your computer and use it in GitHub Desktop.
'combineReducers' from Redux re-implemented
/*
* Demonstrates how 'combineReducers' can be implemented.
*/
function combineReducers(reducers) {
return function(state, action) {
var reducerNames = Object.keys(reducers);
var combination = {};
reducerNames.forEach(function(reducerName) {
combination[reducerName] = reducers[reducerName](state[reducerName], action);
});
return combination;
};
}
/*
* Client code
*/
var state = {
key1: {
subkey1: 'value1'
},
key2: 'value2'
};
var action = 'testAction';
function func1(state, action) {
return state.subkey1 + '_reduced';
}
function key2(state, action) {
return state + '_reduced';
}
var reducer = combineReducers({
key1: func1, key2
});
console.log(reducer(state, action));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment