Ramda js:具有嵌套对象数组的深层嵌套对象的镜头

Gre*_*rds 19 javascript haskell-lens ramda.js

使用Ramda.js(和镜头),我想修改下面的JavaScript对象,将具有ID ="/ 1/B/i"的对象的"NAME:VERSION1"更改为"NAME:VERSION2".

我想使用镜头,因为我只想更改一个深度嵌套的值,但保持整个结构不变.

我不想使用lensIndex,因为我永远不知道数组的顺序,所以相反,我想通过查找它的"id"字段来"找到"数组中的对象.

我可以用镜头做这个,还是应该用不同的方式做?

{
  "id": "/1",
  "groups": [
    {
      "id": "/1/A",
      "apps": [
        {
          "id": "/1/A/i",
          "more nested data skipped to simplify the example": {} 
        }
      ]
    },
    {
      "id": "/1/B",
      "apps": [
        { "id": "/1/B/n", "container": {} },
        {
          "id": "/1/B/i",

          "container": {
            "docker": {
              "image": "NAME:VERSION1",
              "otherStuff": {}
            }
          }
        }
      ]
    }

  ]
}
Run Code Online (Sandbox Code Playgroud)

Sco*_*her 25

这应该是可以通过创建一个与ID匹配的镜头,然后可以与其他镜头组合以向下钻取到图像区域.

首先,我们可以创建一个镜头,专注于匹配某个谓词的数组元素(注意:如果保证匹配列表中的至少一个元素,这将只是一个有效的镜头)

//:: (a -> Boolean) -> Lens [a] a
const lensMatching = pred => (toF => entities => {
    const index = R.findIndex(pred, entities);
    return R.map(entity => R.update(index, entity, entities),
                 toF(entities[index]));
});
Run Code Online (Sandbox Code Playgroud)

请注意,我们在这里手动构建镜头而不是R.lens用于保存重复查找与谓词匹配的项目的索引.

一旦我们有了这个功能,我们就可以构建一个与给定ID相匹配的镜头.

//:: String -> Lens [{ id: String }] { id: String }
const lensById = R.compose(lensMatching, R.propEq('id'))
Run Code Online (Sandbox Code Playgroud)

然后我们可以将所有镜头组合在一起以定位图像区域

const imageLens = R.compose(
  R.lensProp('groups'),
  lensById('/1/B'),
  R.lensProp('apps'),
  lensById('/1/B/i'),
  R.lensPath(['container', 'docker', 'image'])
)
Run Code Online (Sandbox Code Playgroud)

哪个可用于更新data对象,如下所示:

set(imageLens, 'NAME:VERSION2', data)
Run Code Online (Sandbox Code Playgroud)

如果您想要并声明一个专注于图像字符串版本的镜头,您可以更进一步.

const vLens = R.lens(
  R.compose(R.nth(1), R.split(':')),
  (version, str) => R.replace(/:.*/, ':' + version, str)
)

set(vLens, 'v2', 'NAME:v1') // 'NAME:v2'
Run Code Online (Sandbox Code Playgroud)

然后可以将其附加到组合中imageLens以定位整个对象内的版本.

const verLens = compose(imageLens, vLens);
set(verLens, 'VERSION2', data);
Run Code Online (Sandbox Code Playgroud)

  • 这确实很容易理解,并且可以轻松地构成和/或修改。谢谢!对于lensMatching,可以替换为:`function lensMatching(pred){return R.lens(R.find(pred),(newVal,array,other)=> {const index = R.findIndex(pred,array); return R.update(index,newVal,array);}}}`对于我来说,将其与镜头文档联系起来似乎更容易一些。但是,我缺少什么吗? (2认同)

dav*_*ers 9

这是一个解决方案:

const updateDockerImageName =
R.over(R.lensProp('groups'),
       R.map(R.over(R.lensProp('apps'),
                    R.map(R.when(R.propEq('id', '/1/B/i'),
                                 R.over(R.lensPath(['container', 'docker', 'image']),
                                        R.replace(/^NAME:VERSION1$/, 'NAME:VERSION2')))))));
Run Code Online (Sandbox Code Playgroud)

当然,这可以分解为更小的功能.:)

  • 感谢您的回答,这使得更清楚如何使用,地图以及何时使用. (2认同)