Created
August 24, 2016 14:08
-
-
Save baransu/b34002649128f7ce17536ff71ace29b2 to your computer and use it in GitHub Desktop.
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
export function createReducer(initialState, handlers) { | |
const match = (key, type) => ( | |
key | |
.replace(/ /g, '') | |
.split(',') | |
.reduce((acc, k) => ( | |
acc || type.indexOf(k) > -1 ? k : null | |
), null) | |
); | |
const matchKey = (keys, type) => ( | |
keys.indexOf(type) > -1 && type | |
|| keys.reduce((acc, key) => ( | |
key.indexOf(",") > -1 && match(key, type) || acc | |
), null) | |
|| (keys.indexOf("_") > -1 && "_") || "" | |
); | |
return function reducer(state = initialState, action) { | |
const matchedKey = matchKey(Object.keys(handlers), action.type); | |
return handlers.hasOwnProperty(matchedKey) ? handlers[matchedKey](state, action) : state; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment