Last active
March 27, 2017 22:02
-
-
Save jimbol/eba22b7c291c3d379e9c52740fbe461f to your computer and use it in GitHub Desktop.
Transform data with dependencies and overrides
This file contains hidden or 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
const lookUp = ({ hash, ids }) => ids.map((id) => hash[id]); | |
createTransformer({ | |
hash: getHash, | |
ids: getIds, | |
}, lookUp, []); | |
const createTransformer = (dependencies, transformer, fallbackValue) => { | |
return function(state, props, overrides) { | |
const keys = Object.keys(dependencies); | |
const dependenciesHash = keys.reduce((key) => { | |
const dependency = overrides[key] || dependencies[key]; | |
return dependency(state, props); | |
}, {}); | |
const output = transformer(dependenciesHash); | |
if (output == null && fallbackValue != null) { | |
return fallbackValue; | |
} | |
return output; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment