Change multiple objects using the Ramda lens

I currently have a data structure that looks basically like this:

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 am trying to use Ramda to make a request that could do something to change the status of all problems of a particular type.

I am trying to do something according to the composition of the lens through R.map/R.chain, but I cannot process it. Something like this is what I'm 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 bring back the complete set of stateful data.

UPDATE:

I came up with some code that will do what I'm trying to do, but I'm still trying to do each of the steps in the function that can be compiled:

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)
+4
2

, :

//  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);
+4

, , , .

,

const 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": {}}
      ]
    }]
  }]
};

, , . modules, results issues.

, , , status.

:

const modulesLens = lensProp('modules')
const resultsLens = lensProp('results')
const issuesLens = lensProp('issues')
const statusLens = lensProp('status')

, , , , . ( , )

const approveIssues = over(
  modulesLens,
  map(over(
    resultsLens,
    map(over(
      issuesLens,
      map(set(statusLens, 'approve'))
    ))
  ))
)

, , feed approvedIssues , , data.

, .

//Running this
const approvedData = approveIssues(data)

/* Outputs this
{
  "id": 123,
  "modules": [{
    "id": 1,
    "results": [{
      "issues": [
        {"data": {}, "status": "approve", "type": "change"},
        {"data": {}, "status": "approve", "type": "remove"},
        {"data": {}, "status": "approve", "type": "change"}
      ],
      "status": "fail"
    }]}, {
      "id": 2,
      "results": [{
        "issues": [
          {"data": {}, "status": "approve", "type": "change"},
          {"data": {}, "status": "approve", "type": "remove"},
          {"data": {}, "status": "approve", "type": "change"}],
        "status": "pass"
    }]}
  ]}
*/

compose, , .

+3

Source: https://habr.com/ru/post/1655727/


All Articles