Skip to content

Instantly share code, notes, and snippets.

@abiodun0
Last active June 21, 2017 22:22
Show Gist options
  • Save abiodun0/6fe27602c8c26e78fd2458c2d03473cf to your computer and use it in GitHub Desktop.
Save abiodun0/6fe27602c8c26e78fd2458c2d03473cf to your computer and use it in GitHub Desktop.
Denormalize with ramda and redux state
import { compose, mapObjIndexed, values } from 'ramda';
export const denormalize = compose(
values,
mapObjIndexed((val, key) => ({ ...val, id: key }))
);
//the ramda part might be confusing at first glance. `mapObjIndexed`
// let's me access the object key and stuff it in the object itself, and
// `values` drops all keys and converts it to an array. `compose` runs both those functions from right to left.
// `ramda` curries by default, so after i build the function all i need to do is pass it data
const selector = createSelector(
state => state.editor,
editor => ({
editor: {
...editor,
nodes: denormalize(editor.nodes),
},
})
);
// `denormalize` pushes that object -> array
// mapping outside of your react components
// and since it's in a selector, the transformation is optimized
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment