Skip to content

Instantly share code, notes, and snippets.

@jimbol
Last active March 27, 2017 22:02
Show Gist options
  • Save jimbol/eba22b7c291c3d379e9c52740fbe461f to your computer and use it in GitHub Desktop.
Save jimbol/eba22b7c291c3d379e9c52740fbe461f to your computer and use it in GitHub Desktop.
Transform data with dependencies and overrides
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