Last active
August 31, 2017 19:53
-
-
Save hrldcpr/c17dcf39451d5a8867efbcdb804fefaf to your computer and use it in GitHub Desktop.
exampda
This file contains 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
// We have some state like: | |
const state1 = { | |
data: { some: 'stuff' }, | |
people: ['blah', 'blah', 'blah'], | |
memberships: [ | |
{ id: 1, roles: [], other: 'stuff' }, | |
{ id: 2, roles: ['client'], more: 'junk' } | |
] | |
}; | |
// We want to be able to add a role to a particular membership (without mutation): | |
import { append, evolve, map, propEq, when } from 'ramda'; | |
const addRole = (id, role) => evolve({ | |
memberships: map(when(propEq('id', id), evolve({ roles: append(role) }))) | |
}); | |
// For example: | |
addRole(2, 'admin')(state1) | |
// …returns a new object, in which `data`, `people`, and membership id 1 are all the exact same objects as before (not copies): | |
{ | |
data: { some: 'stuff' }, | |
people: ['blah', 'blah', 'blah'], | |
memberships: [ | |
{ id: 1, roles: [], other: 'stuff' }, | |
{ id: 2, roles: ['client', 'admin'], more: 'junk' } | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was curious how to prevent adding 'admin' multiple times and came up with the following:
it gets a bit more funky with that… but still works!
also what about full-on currying
addRole
and maybe switching the role and id params?I messed around with it in a codepen.