Last active
June 21, 2017 22:22
-
-
Save abiodun0/6fe27602c8c26e78fd2458c2d03473cf to your computer and use it in GitHub Desktop.
Denormalize with ramda and redux state
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
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 |
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 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