Skip to content

Instantly share code, notes, and snippets.

@Swivelgames
Created April 6, 2017 19:04
Show Gist options
  • Save Swivelgames/c068e513b5cce4e5cde0c315da9bd11f to your computer and use it in GitHub Desktop.
Save Swivelgames/c068e513b5cce4e5cde0c315da9bd11f to your computer and use it in GitHub Desktop.
React Utility: mapChildrenToObject
const mapChildrenToObject = (children) => (
children.reduce((v, cV) => {
let { name } = cV.type;
if (!name) return v;
if (name === 'Connect') {
name = cV.type.displayName.replace(/Connect\((\w+)\)/g, '$1');
}
return Object.assign({}, v,
v[cV.type.name] ? {
[cV.type.name]: [].concat(v[cV.type.name], cV)
} : {
[cV.type.name]: cV
}
);
}, {})
);
export default mapChildrenToObject;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment