Reputation: 11197
I want to have a pipeline which will set up a location object. Suppose the location object looks as follows to start:
{
pathname: '/users',
search: 'sort=desc&sortBy=name',
hash: '',
state: {},
}
On step one of the pipeline, I want to set the state.redirect
of this to be the current object, but without the state
included. e.g.,
{
pathname: '/users',
search: 'sort=desc&sortBy=name',
hash: '',
state: {
redirect: {
pathname: '/users',
search: 'sort=desc&sortBy=name',
hash: '',
}
}
}
I started with the following, but it didn't really work.
set(lensProp('state'), /* ??? */)
what's the appropriate way to do this in ramda?
Upvotes: 0
Views: 73
Reputation: 64943
What about the following approach?
const input = {
pathname: '/users',
search: 'sort=desc&sortBy=name',
hash: '',
state: {},
}
const { lensProp, set, over, dissoc, pipe } = R
// The W combinator to apply twice the same input to a binary function
// W :: (a -> a -> b) -> a -> c
const W = f => x => f (x) (x)
// stateLens :: a -> b
const stateLens = lensProp ('state')
// Note how W will apply the input twice: it'll set the input
// to 'state' property!
// setSelfState :: (Object -> Object -> Object) -> Object -> Object
const setSelfState = W (set (stateLens))
// dissocInnerState :: Object -> Object
const dissocInnerState = over (stateLens) (dissoc ('state'))
// setSelftState :: Object -> Object
const setSelfState_ = pipe (
setSelfState,
dissocInnerState
)
const output = setSelfState_ (input)
console.log (output)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
Upvotes: 0
Reputation: 50807
This should do it:
const loc = {
pathname: '/users',
search: 'sort=desc&sortBy=name',
hash: '',
state: {},
}
console.log(R.dissoc('state', loc))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
Ramda's What Function Should I Use? might help find functions like this.
Upvotes: 1