Shard Shard - 2 months ago 17
Javascript Question

Changing multiple objects using Ramda lens

I currently have a data structure that looks mostly like that following:

var data = { "id": 123,
"modules": [{
"id": 1,
"results": [{
"status": "fail",
"issues": [
{"type": "change", "status": "warn", "data": {}},
{"type": "remove", "status": "warn", "data": {}},
{"type": "change", "status": "warn", "data": {}}
]
}]
},{
"id": 2
"results": [{
"status": "pass",
"issues": [
{"type": "change", "status": "warn", "data": {}},
{"type": "remove", "status": "warn", "data": {}},
{"type": "change", "status": "warn", "data": {}}
]
}]
}]
}


I have been trying to use Ramda to make a compose query that could do something like, change the status of all issues that are of a certain type.

I have been trying to do something along the lines of composing the lens's through R.map/R.chain but I can't seem to work it out. Something like this is what im trying to do:

let approvedData = R.compose(
R.set(R.__, 'approve', Data)
R.lensProp('status')
R.map(R.lensIndex),
R.lensProp('issues'),
R.map(R.lensIndex),
R.lensProp('results'),
R.map(R.lensIndex),
R.lensProp('modules')
)(Data)


And have it return back the full data set with the statuses changed.

UPDATE:

I have come up with some code that will do what I'm trying to do, but I'm still struggling to make each of the steps into functions that can then be composed:

R.over(R.lensProp('modules'), R.map(
R.over(R.lensProp('results'), R.map(
R.over(R.lensProp('issues'), R.map(
R.set(R.lensProp('status'), 'approve')
))
))
), Data)

Answer

Your updated solution looks fine to me, but it is possible to create the transformation function via composition:

//  update$modules$results$issues$status :: (String -> String) -> Object -> Object
var update$modules$results$issues$status = R.compose(
  R.over(R.lensProp('modules')),
  R.map,
  R.over(R.lensProp('results')),
  R.map,
  R.over(R.lensProp('issues')),
  R.map,
  R.over(R.lensProp('status'))
);

update$modules$results$issues$status(R.always('approve'))(data);
Comments